bedrock123 commited on
Commit
746c72a
1 Parent(s): 8872483

Update burrito.py

Browse files
Files changed (1) hide show
  1. 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 = 'path/to/train/folder'
14
- val_dir = 'path/to/validation/folder'
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
- # Flatten and add dense layers
 
 
54
  model.add(Flatten())
55
  model.add(Dense(512, activation='relu'))
56
  model.add(Dropout(0.5))
57
- model.add(Dense(3, activation='softmax'))
 
 
 
 
 
58
 
59
- # Compile the model
60
- model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 
61
 
62
- # Train the model
63
- history = model.fit(
64
  train_generator,
65
- steps_per_epoch=train_generator.samples // BATCH_SIZE,
66
- epochs=50,
67
  validation_data=val_generator,
68
- validation_steps=val_generator.samples // BATCH_SIZE)
69
-
70
- # Load an image
71
- img_path = 'path/to/image.jpg'
72
- img = image.load_img(img_path, target_size=(IMG_SIZE, IMG_SIZE))
73
-
74
- # Convert the image to a numpy array and normalize it
75
- img_array = image.img_to_array(img) / 255.
76
-
77
- # Expand the dimensions of the array to match the model input shape
78
- img_array = np.expand_dims(img_array, axis=0)
79
-
80
- # Use the model to predict the class probabilities
81
- probs = model.predict(img_array)[0]
82
-
83
- # Print the predicted class probabilities
84
- print(probs)
 
 
 
 
 
 
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))