-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageClassification.py
executable file
·119 lines (87 loc) · 4.01 KB
/
ImageClassification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import csv
import scipy
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions, ResNet50
from keras.preprocessing import image
from keras import regularizers
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D, BatchNormalization, Dropout, Flatten
from keras import backend as K
## set train, validation, and test paths
train_path = '/Users/dikshabhandari/Desktop/sudhir/GroceryStoreDataset-master/dataset/train'
validation_path = '/Users/dikshabhandari/Desktop/sudhir/GroceryStoreDataset-master/dataset/val'
test_path = '/Users/dikshabhandari/Desktop/sudhir/GroceryStoreDataset-master/dataset/test'
#preprocessing the data
train_batches = ImageDataGenerator().flow_from_directory(
train_path, target_size=(224,224), classes = ['Fruits', 'Packages', 'Vegetables'], batch_size = 30)
validation_batches = ImageDataGenerator().flow_from_directory(
validation_path, target_size=(224,224), classes = ['Fruits', 'Packages', 'Vegetables'], batch_size = 10)
test_batches = ImageDataGenerator().flow_from_directory(
test_path, target_size=(224,224), classes = ['Fruits', 'Packages', 'Vegetables'], batch_size = 10)
#creating Convolutional neural network
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(224, 224, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(3, activation='sigmoid'))
#model.summary()
#compile and run
## set steps per epoch for train
train_filenames = train_batches.filenames
steps_train = len(train_filenames)/train_batches.batch_size
#print(steps_train)
## set steps per epoch for validation
validation_filenames = validation_batches.filenames
steps_valid = len(validation_filenames)/validation_batches.batch_size
#print(steps_valid)
model.compile(loss='categorical_crossentropy',
optimizer= SGD(learning_rate = 1e-4), metrics = 'categorical_accuracy')
model.save('/Users/dikshabhandari/Desktop/sudhir/baseline_4layer.h5', include_optimizer=True, save_traces=True)
# fit_generator1 = model.fit(
# train_batches,
# steps_per_epoch=steps_train,
# epochs=20,
# validation_data = validation_batches,
# validation_steps=steps_valid)
# plt.plot(fit_generator1.history['loss'])
# plt.plot(fit_generator1.history['val_loss'])
# plt.title('Model loss-SGD')
# plt.ylabel('loss')
# plt.xlabel('epoch')
# plt.legend(['train', 'test'], loc='upper right')
# plt.show()
# plt.plot(fit_generator1.history['categorical_accuracy'])
# plt.plot(fit_generator1.history['val_categorical_accuracy'])
# plt.title('Model accuracy-SGD')
# plt.ylabel('accuracy')
# plt.xlabel('epoch')
# plt.legend(['train', 'test'], loc='upper right')
# plt.show()
# def classify(img_path):
# img = image.load_img(img_path, target_size=(224, 224))
# img_array = image.img_to_array(img)
# img_batch = np.expand_dims(img_array, axis=0)
# img_preprocessed = preprocess_input(img_batch)
# #model = tf.keras.applications.resnet50.ResNet50()
# prediction = model.predict(img_preprocessed)
# print(prediction)
# classify("/Users/dikshabhandari/Desktop/sudhir/GroceryStoreDataset-master/sample_images/iconic/Alpro-Fresh-Soy-Milk_Iconic.jpg")