okeowo1014 commited on
Commit
e1ede78
1 Parent(s): 73dc6d8

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. cat_dog_classifier.keras +3 -0
  3. predictor.py +32 -0
  4. train.py +78 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ cat_dog_classifier.keras filter=lfs diff=lfs merge=lfs -text
cat_dog_classifier.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a199caa07ed268b554abeba28751f69eedb73ba58dc1ee4984bb320dab1c1d3
3
+ size 59234141
predictor.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 # Assuming you have OpenCV installed
2
+ import numpy as np
3
+ from tensorflow.keras.preprocessing import image
4
+ import tensorflow as tf
5
+ # Load the saved model
6
+ model = tf.keras.models.load_model('cat_dog_classifier.keras') # Replace with your model filename
7
+ img_width, img_height = 224, 224 # VGG16 expects these dimensions
8
+
9
+
10
+ # Function to preprocess an image for prediction
11
+ def preprocess_image(img_path):
12
+ img = cv2.imread(img_path) # Read the image
13
+ img = cv2.resize(img, (img_width, img_height)) # Resize according to model input size
14
+ img = img.astype('float32') / 255.0 # Normalize pixel values
15
+ img = np.expand_dims(img, axis=0) # Add a batch dimension (model expects batch of images)
16
+ return img
17
+
18
+
19
+ # Get the path to your new image
20
+ new_image_path = 'test1/11.jpg' # Replace with your image path
21
+
22
+ # Preprocess the image
23
+ preprocessed_image = preprocess_image(new_image_path)
24
+
25
+ # Make prediction
26
+ prediction = model.predict(preprocessed_image)
27
+
28
+ # Decode the prediction (assuming class 0 is cat, 1 is dog)
29
+ predicted_class = int(prediction[0][0] > 0.5) # Threshold of 0.5 for binary classification
30
+ class_names = ['cat', 'dog'] # Adjust class names according to your model
31
+
32
+ print(f"Predicted class: {class_names[predicted_class]}")
train.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
3
+ from tensorflow.keras.applications import VGG16
4
+ from tensorflow.keras.layers import Flatten, Dense
5
+
6
+ # Define data paths (modify as needed)
7
+ train_data_dir = 'tt'
8
+ validation_data_dir = 'valid'
9
+ test_data_dir = 'valid'
10
+
11
+ # Set image dimensions (adjust if necessary)
12
+ img_width, img_height = 224, 224 # VGG16 expects these dimensions
13
+
14
+ # Data augmentation for improved generalization (optional)
15
+ train_datagen = ImageDataGenerator(
16
+ rescale=1./255, # Normalize pixel values
17
+ shear_range=0.2,
18
+ zoom_range=0.2,
19
+ horizontal_flip=True,
20
+ fill_mode='nearest'
21
+ )
22
+
23
+ validation_datagen = ImageDataGenerator(rescale=1./255) # Only rescale for validation
24
+
25
+ # Load training and validation data
26
+ train_generator = train_datagen.flow_from_directory(
27
+ train_data_dir,
28
+ target_size=(img_width, img_height),
29
+ batch_size=32, # Adjust batch size based on GPU memory
30
+ class_mode='binary' # Two classes: cat or dog
31
+ )
32
+
33
+ validation_generator = validation_datagen.flow_from_directory(
34
+ validation_data_dir,
35
+ target_size=(img_width, img_height),
36
+ batch_size=32,
37
+ class_mode='binary'
38
+ )
39
+
40
+ # Load pre-trained VGG16 model (without the top layers)
41
+ base_model = VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
42
+
43
+ # Freeze the base model layers (optional - experiment with unfreezing for fine-tuning)
44
+ base_model.trainable = False
45
+
46
+ # Add custom layers for classification on top of the pre-trained model
47
+ x = base_model.output
48
+ x = Flatten()(x)
49
+ predictions = Dense(1, activation='sigmoid')(x) # Sigmoid for binary classification
50
+
51
+ # Create the final model
52
+ model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
53
+
54
+ # Compile the model
55
+ model.compile(loss='binary_crossentropy',
56
+ optimizer='adam',
57
+ metrics=['accuracy'])
58
+
59
+ # Train the model
60
+ history = model.fit(
61
+ train_generator,
62
+ epochs=3, # Adjust number of epochs based on dataset size and validation performance
63
+ validation_data=validation_generator
64
+ )
65
+
66
+ # Evaluate the model on test data (optional)
67
+ test_generator = validation_datagen.flow_from_directory(
68
+ test_data_dir,
69
+ target_size=(img_width, img_height),
70
+ batch_size=32,
71
+ class_mode='binary'
72
+ )
73
+
74
+ test_loss, test_acc = model.evaluate(test_generator)
75
+ print('Test accuracy:', test_acc)
76
+
77
+ # Save the model for future use (optional)
78
+ model.save('cat_dog_classifier2.keras')