fcrescio commited on
Commit
0abe916
·
verified ·
1 Parent(s): ac85887

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +33 -5
README.md CHANGED
@@ -29,28 +29,56 @@ Inference
29
  To use this model for inference, you can load it using Hugging Face's from_pretrained functionality and pass in an image for orientation prediction.
30
 
31
  ```python
32
- from PIL import Image
33
  import torch
34
- from transformers import SimpleCNN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Load the model
37
- model = SimpleCNN.from_pretrained("path_to_model")
 
 
 
38
 
39
  # Function to predict orientation
40
  def predict_orientation(image_path, model):
41
  img = Image.open(image_path).convert('L') # Load image in grayscale
42
  img = img.resize((128, 128)) # Resize to 128x128
43
- img_tensor = torch.tensor(np.array(img) / 255.0).unsqueeze(0).unsqueeze(0)
44
  with torch.no_grad():
45
  output = model(img_tensor)
46
  is_rotated = torch.argmax(output, dim=1).item() == 1
47
  return "Rotated" if is_rotated else "Normal"
48
 
49
  # Example usage
50
- result = predict_orientation("example_image.jpg", model)
51
  print(f"Image Orientation: {result}")
52
  ```
53
 
 
 
54
  ## Training
55
  The model was trained using standard binary cross-entropy loss and an Adam optimizer. It was trained on grayscale images resized to 128x128 pixels.
56
 
 
29
  To use this model for inference, you can load it using Hugging Face's from_pretrained functionality and pass in an image for orientation prediction.
30
 
31
  ```python
 
32
  import torch
33
+ import torch.nn as nn
34
+ import torch.nn.functional as F
35
+ from safetensors.torch import load_file
36
+ from PIL import Image
37
+ import numpy as np
38
+
39
+ # Define the corrected SimpleCNN architecture
40
+ class SimpleCNN(nn.Module):
41
+ def __init__(self):
42
+ super(SimpleCNN, self).__init__()
43
+ self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1) # Adjusted to 16 output channels
44
+ self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1) # Adjusted to 32 output channels
45
+ self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1) # Adjusted to 32 output channels
46
+ self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
47
+ self.fc1 = nn.Linear(32 * 16 * 16, 32) # Adjusted input and output dimensions
48
+ self.fc2 = nn.Linear(32, 2) # Adjusted input dimension
49
+
50
+ def forward(self, x):
51
+ x = self.pool(F.relu(self.conv1(x)))
52
+ x = self.pool(F.relu(self.conv2(x)))
53
+ x = self.pool(F.relu(self.conv3(x)))
54
+ x = x.view(x.size(0), -1) # Flatten
55
+ x = F.relu(self.fc1(x))
56
+ x = self.fc2(x)
57
+ return x
58
 
59
  # Load the model
60
+ model = SimpleCNN()
61
+ state_dict = load_file("model.safetensors")
62
+ model.load_state_dict(state_dict)
63
+ model.eval()
64
 
65
  # Function to predict orientation
66
  def predict_orientation(image_path, model):
67
  img = Image.open(image_path).convert('L') # Load image in grayscale
68
  img = img.resize((128, 128)) # Resize to 128x128
69
+ img_tensor = torch.tensor(np.array(img) / 255.0, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
70
  with torch.no_grad():
71
  output = model(img_tensor)
72
  is_rotated = torch.argmax(output, dim=1).item() == 1
73
  return "Rotated" if is_rotated else "Normal"
74
 
75
  # Example usage
76
+ result = predict_orientation("example.jpg", model)
77
  print(f"Image Orientation: {result}")
78
  ```
79
 
80
+ (HT: https://huggingface.co/khasinski)
81
+
82
  ## Training
83
  The model was trained using standard binary cross-entropy loss and an Adam optimizer. It was trained on grayscale images resized to 128x128 pixels.
84