MuhammadAhmad21042002 commited on
Commit
9f1467e
·
verified ·
1 Parent(s): c6577f8

Create app.p

Browse files
Files changed (1) hide show
  1. app.p +68 -0
app.p ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras.models import load_model
2
+ import numpy as np
3
+
4
+ # Load the saved model
5
+ model = load_model('model.h5')
6
+
7
+ predicted_image = model.predict(np.expand_dims(input_image, axis=0))
8
+
9
+ import tensorflow as tf
10
+ from tensorflow.keras.datasets import cifar10
11
+ from tensorflow.keras.models import Sequential
12
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
13
+ from tensorflow.keras.utils import to_categorical
14
+ from tensorflow.keras.optimizers import Adam
15
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
16
+
17
+ # Load CIFAR-10 dataset
18
+ (x_train, y_train), (x_test, y_test) = cifar10.load_data()
19
+
20
+ # Normalize pixel values to be between 0 and 1
21
+ x_train = x_train.astype('float32') / 255.0
22
+ x_test = x_test.astype('float32') / 255.0
23
+
24
+ # One-hot encode the labels
25
+ y_train = to_categorical(y_train, num_classes=10)
26
+ y_test = to_categorical(y_test, num_classes=10)
27
+
28
+ predicted = model.predict(x_test)
29
+
30
+ np.argmax(predicted, axis = 1)
31
+
32
+ from PIL import Image
33
+ import numpy as np
34
+
35
+ classes = {
36
+ 0 : 'Airplane',
37
+ 1 : 'Automobile',
38
+ 2 : 'Bird',
39
+ 3 : 'Cat',
40
+ 4 : 'Deer',
41
+ 5 : 'Dog',
42
+ 6 : 'Frog',
43
+ 7 : 'Horse',
44
+ 8 : 'Ship',
45
+ 9 : 'Truck'
46
+ }
47
+
48
+ def prediction(input_img):
49
+ # Define the transformation
50
+ transform = transforms.Compose([
51
+ transforms.Resize(32),
52
+ transforms.CenterCrop(32),
53
+ transforms.ToTensor(),
54
+ # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
55
+ ])
56
+ pil_image = Image.fromarray(input_img.astype('uint8'))
57
+ # Apply the transformation
58
+ transformed_image = np.array(transform(pil_image).T)
59
+ input_image = np.expand_dims(transformed_image, axis=0)
60
+ output = model.predict(input_image)
61
+ # print(transformed_image.shape)
62
+ # print(transformed_image)
63
+ # plt.imshow(transformed_image)
64
+ # plt.show()
65
+ return classes[np.argmax(output)]
66
+ demo = gr.Interface(prediction, gr.Image(), "text")
67
+ demo.launch()
68
+