bedrock123
commited on
Commit
•
746c72a
1
Parent(s):
8872483
Update burrito.py
Browse files- burrito.py +43 -29
burrito.py
CHANGED
@@ -10,8 +10,8 @@ IMG_SIZE = 224
|
|
10 |
BATCH_SIZE = 32
|
11 |
|
12 |
# Define train and validation directories
|
13 |
-
train_dir = '
|
14 |
-
val_dir = '
|
15 |
|
16 |
# Use ImageDataGenerator for data augmentation
|
17 |
train_datagen = ImageDataGenerator(
|
@@ -39,46 +39,60 @@ val_generator = val_datagen.flow_from_directory(
|
|
39 |
batch_size=BATCH_SIZE,
|
40 |
class_mode='categorical')
|
41 |
|
42 |
-
# Define the model
|
43 |
model = Sequential()
|
44 |
|
45 |
-
# Add convolutional layers
|
46 |
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)))
|
47 |
model.add(MaxPooling2D((2, 2)))
|
|
|
48 |
model.add(Conv2D(64, (3, 3), activation='relu'))
|
49 |
model.add(MaxPooling2D((2, 2)))
|
|
|
50 |
model.add(Conv2D(128, (3, 3), activation='relu'))
|
51 |
model.add(MaxPooling2D((2, 2)))
|
52 |
|
53 |
-
|
|
|
|
|
54 |
model.add(Flatten())
|
55 |
model.add(Dense(512, activation='relu'))
|
56 |
model.add(Dropout(0.5))
|
57 |
-
model.add(Dense(
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
-
|
|
|
61 |
|
62 |
-
# Train the model
|
63 |
-
history = model.
|
64 |
train_generator,
|
65 |
-
steps_per_epoch=
|
66 |
-
epochs=
|
67 |
validation_data=val_generator,
|
68 |
-
validation_steps=
|
69 |
-
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
img_array =
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
10 |
BATCH_SIZE = 32
|
11 |
|
12 |
# Define train and validation directories
|
13 |
+
train_dir = 'm2rncvif2arzs1w3q44gfn\images.cv_m2rncvif2arzs1w3q44gfn\data\train\burrito'
|
14 |
+
val_dir = 'm2rncvif2arzs1w3q44gfn\images.cv_m2rncvif2arzs1w3q44gfn\data\val\burrito'
|
15 |
|
16 |
# Use ImageDataGenerator for data augmentation
|
17 |
train_datagen = ImageDataGenerator(
|
|
|
39 |
batch_size=BATCH_SIZE,
|
40 |
class_mode='categorical')
|
41 |
|
42 |
+
# Define the model architecture
|
43 |
model = Sequential()
|
44 |
|
|
|
45 |
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)))
|
46 |
model.add(MaxPooling2D((2, 2)))
|
47 |
+
|
48 |
model.add(Conv2D(64, (3, 3), activation='relu'))
|
49 |
model.add(MaxPooling2D((2, 2)))
|
50 |
+
|
51 |
model.add(Conv2D(128, (3, 3), activation='relu'))
|
52 |
model.add(MaxPooling2D((2, 2)))
|
53 |
|
54 |
+
model.add(Conv2D(256, (3, 3), activation='relu'))
|
55 |
+
model.add(MaxPooling2D((2, 2)))
|
56 |
+
|
57 |
model.add(Flatten())
|
58 |
model.add(Dense(512, activation='relu'))
|
59 |
model.add(Dropout(0.5))
|
60 |
+
model.add(Dense(2, activation='softmax'))
|
61 |
+
|
62 |
+
# Compile the model with categorical crossentropy loss and Adam optimizer
|
63 |
+
model.compile(loss='categorical_crossentropy',
|
64 |
+
optimizer='adam',
|
65 |
+
metrics=['accuracy'])
|
66 |
|
67 |
+
# Define the number of training and validation steps per epoch
|
68 |
+
train_steps_per_epoch = train_generator.samples // BATCH_SIZE
|
69 |
+
val_steps_per_epoch = val_generator.samples // BATCH_SIZE
|
70 |
|
71 |
+
# Train the model with fit_generator
|
72 |
+
history = model.fit_generator(
|
73 |
train_generator,
|
74 |
+
steps_per_epoch=train_steps_per_epoch,
|
75 |
+
epochs=10,
|
76 |
validation_data=val_generator,
|
77 |
+
validation_steps=val_steps_per_epoch)
|
78 |
+
|
79 |
+
# Path to directory with burrito images
|
80 |
+
dir_path = 'm2rncvif2arzs1w3q44gfn\images.cv_m2rncvif2arzs1w3q44gfn\data\test\burrito'
|
81 |
+
|
82 |
+
# Loop through all images in the directory
|
83 |
+
for img_file in os.listdir(dir_path):
|
84 |
+
# Load and preprocess the image
|
85 |
+
img_path = os.path.join(dir_path, img_file)
|
86 |
+
img = image.load_img(img_path, target_size=(IMG_SIZE, IMG_SIZE))
|
87 |
+
img_array = image.img_to_array(img)
|
88 |
+
img_array = np.expand_dims(img_array, axis=0)
|
89 |
+
img_array /= 255.0
|
90 |
+
|
91 |
+
# Make a prediction
|
92 |
+
prediction = model.predict(img_array)
|
93 |
+
|
94 |
+
# Print the prediction result
|
95 |
+
if prediction[0][0] > prediction[0][1]:
|
96 |
+
print('{}: Not a burrito'.format(img_file))
|
97 |
+
else:
|
98 |
+
print('{}: Burrito!'.format(img_file))
|