Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,108 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
**Description**
|
| 6 |
+
|
| 7 |
+
This model demonstrates the capabilities of a CNN for classifying screenshots as game/reality, using Minecraft as an example:
|
| 8 |
+
|
| 9 |
+
- 0 class - Minecraft.
|
| 10 |
+
|
| 11 |
+
- 1 class - Not Minecraft.
|
| 12 |
+
|
| 13 |
+
**Main Idea**
|
| 14 |
+
|
| 15 |
+
The hypothesis was that the game Minecraft has distinct 3D voxel graphics, which would allow the model to distinguish photos from the game from photos taken in real life.
|
| 16 |
+
|
| 17 |
+
The model was trained on 4,500 screenshot photos from the game and approximately 3,800 photos from real life and other games.
|
| 18 |
+
|
| 19 |
+
**Characteristics**
|
| 20 |
+
|
| 21 |
+
The model is a 4‑layer and 3-layer convolutional networks and two linear MLP layers for classification.
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
**Tests**
|
| 25 |
+
|
| 26 |
+
The first test was conducted on vanilla Minecraft/real photos.
|
| 27 |
+
|
| 28 |
+
Layer count | F1 | Accuracy |
|
| 29 |
+
| :--- | :--- | :--- |
|
| 30 |
+
| **3**|**0.94** | **0.94**|
|
| 31 |
+
| **4**|**0.96** | **0.96**|
|
| 32 |
+
|
| 33 |
+
The second test was conducted on Minecraft (shaders, texture packs, and vanilla version)/ real photo, photos of voxel games (3D cube world, Dragon Quest Builders).
|
| 34 |
+
|
| 35 |
+
Layer count | F1 | Accuracy |
|
| 36 |
+
| :--- | :--- | :--- |
|
| 37 |
+
| **3**|**0.83** | **0.83**|
|
| 38 |
+
| **4**|**0.82** | **0.82**|
|
| 39 |
+
|
| 40 |
+
The test results show that the Minecraft/Reality task is simpler for CNN than Minecraft/Similar 3D voxel games. The hypothesis was confirmed for simple examples. For more complex tasks, other methods are required, including increasing the dataset size, changing the problem statement, and providing a better variety of training data.
|
| 41 |
+
|
| 42 |
+
**Example of inference**
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from transformers import AutoModel
|
| 46 |
+
import torch
|
| 47 |
+
from torchvision import transforms
|
| 48 |
+
from PIL import Image
|
| 49 |
+
from transformers import AutoConfig, AutoModelForImageClassification
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def open_image(image_path):
|
| 53 |
+
image = Image.open(image_path).convert('RGB')
|
| 54 |
+
|
| 55 |
+
image_ten = transform(image)
|
| 56 |
+
|
| 57 |
+
return image_ten
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_prediction(model, image):
|
| 61 |
+
model.eval()
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
pred = model(image)
|
| 64 |
+
print(pred)
|
| 65 |
+
out = torch.softmax(pred, dim=1)
|
| 66 |
+
print(out)
|
| 67 |
+
out = torch.argmax(out, dim=1)
|
| 68 |
+
print(out)
|
| 69 |
+
|
| 70 |
+
return out
|
| 71 |
+
|
| 72 |
+
repo_id = 'Neweret/minecraft_screenshot_classifier'
|
| 73 |
+
|
| 74 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 75 |
+
|
| 76 |
+
model = AutoModelForImageClassification.from_pretrained(
|
| 77 |
+
repo_id,
|
| 78 |
+
trust_remote_code = True
|
| 79 |
+
).to(device)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
transform = transforms.Compose([
|
| 83 |
+
transforms.Resize((224,224)),
|
| 84 |
+
transforms.ToTensor(),
|
| 85 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 86 |
+
])
|
| 87 |
+
|
| 88 |
+
print('Write the image path: ')
|
| 89 |
+
image_path = input().strip()
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
image_ten = open_image(image_path).to(device).unsqueeze(0)
|
| 93 |
+
|
| 94 |
+
except AttributeError:
|
| 95 |
+
print('Error: Incorrect value type!')
|
| 96 |
+
exit()
|
| 97 |
+
|
| 98 |
+
except FileNotFoundError:
|
| 99 |
+
print('Error: File not exist!')
|
| 100 |
+
exit()
|
| 101 |
+
|
| 102 |
+
pred = get_prediction(model, image_ten)
|
| 103 |
+
|
| 104 |
+
if pred == 0:
|
| 105 |
+
print('It`s a Minecraft')
|
| 106 |
+
else:
|
| 107 |
+
print('It`s not Minecraft')
|
| 108 |
+
```
|