Spaces:
Running
Running
Commit
·
977cc9d
1
Parent(s):
58bd1b2
Upload utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.models import Sequential
|
| 2 |
+
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Dropout, SpatialDropout2D
|
| 3 |
+
from tensorflow.keras.losses import sparse_categorical_crossentropy, binary_crossentropy
|
| 4 |
+
from tensorflow.keras.optimizers import Adam
|
| 5 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
def gen_labels():
|
| 10 |
+
train = 'Dataset/Train'
|
| 11 |
+
train_generator = ImageDataGenerator(rescale = 1/255)
|
| 12 |
+
|
| 13 |
+
train_generator = train_generator.flow_from_directory(train,
|
| 14 |
+
target_size = (300,300),
|
| 15 |
+
batch_size = 32,
|
| 16 |
+
class_mode = 'sparse')
|
| 17 |
+
labels = (train_generator.class_indices)
|
| 18 |
+
labels = dict((v,k) for k,v in labels.items())
|
| 19 |
+
|
| 20 |
+
return labels
|
| 21 |
+
|
| 22 |
+
def preprocess(image):
|
| 23 |
+
image = np.array(image.resize((256, 256), Image.LANCZOS))
|
| 24 |
+
image = np.array(image, dtype='uint8')
|
| 25 |
+
image = np.array(image) / 255.0
|
| 26 |
+
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
def model_arc():
|
| 30 |
+
model = Sequential()
|
| 31 |
+
|
| 32 |
+
# Convolution blocks
|
| 33 |
+
model.add(Conv2D(32, kernel_size=(3,3), padding='same', input_shape=(300,300,3), activation='relu'))
|
| 34 |
+
model.add(MaxPooling2D(pool_size=2))
|
| 35 |
+
|
| 36 |
+
model.add(Conv2D(64, kernel_size=(3,3), padding='same', activation='relu'))
|
| 37 |
+
model.add(MaxPooling2D(pool_size=2))
|
| 38 |
+
|
| 39 |
+
model.add(Conv2D(32, kernel_size=(3,3), padding='same', activation='relu'))
|
| 40 |
+
model.add(MaxPooling2D(pool_size=2))
|
| 41 |
+
|
| 42 |
+
# Classification layers
|
| 43 |
+
model.add(Flatten())
|
| 44 |
+
|
| 45 |
+
model.add(Dense(64, activation='relu'))
|
| 46 |
+
model.add(Dropout(0.2))
|
| 47 |
+
model.add(Dense(32, activation='relu'))
|
| 48 |
+
|
| 49 |
+
model.add(Dropout(0.2))
|
| 50 |
+
model.add(Dense(6, activation='softmax'))
|
| 51 |
+
|
| 52 |
+
return model
|