Nekshay commited on
Commit
588994d
1 Parent(s): 21b90ca

Update Train_code_mobilenet

Browse files
Files changed (1) hide show
  1. Train_code_mobilenet +95 -0
Train_code_mobilenet CHANGED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.applications import MobileNet
3
+ from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
4
+ from tensorflow.keras.models import Model
5
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
6
+
7
+ # Load the MobileNet base model
8
+ base_model = MobileNet(weights='imagenet', include_top=False)
9
+
10
+ # Add custom classification layers
11
+ x = base_model.output
12
+ x = GlobalAveragePooling2D()(x)
13
+ x = Dense(1024, activation='relu')(x)
14
+ num_classes=2
15
+ predictions = Dense(num_classes, activation='softmax')(x)
16
+
17
+ model = Model(inputs=base_model.input, outputs=predictions)
18
+
19
+ # Compile the model
20
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
21
+
22
+ # Data augmentation and preprocessing
23
+
24
+
25
+ train_datagen = ImageDataGenerator(
26
+ preprocessing_function=tf.keras.applications.mobilenet.preprocess_input,
27
+ rotation_range=20,
28
+ width_shift_range=0.2,
29
+ height_shift_range=0.2,
30
+ horizontal_flip=True
31
+ )
32
+ batch_size=16
33
+ train_generator = train_datagen.flow_from_directory(
34
+ '/content/tire-dataset/train_data',
35
+ target_size=(224, 224),
36
+ batch_size=batch_size,
37
+ class_mode='categorical'
38
+ )
39
+
40
+ test_datagen = ImageDataGenerator(
41
+ preprocessing_function=tf.keras.applications.mobilenet.preprocess_input,
42
+ rotation_range=20,
43
+ width_shift_range=0.2,
44
+ height_shift_range=0.2,
45
+ horizontal_flip=True
46
+ )
47
+ batch_size=16
48
+
49
+
50
+ # Train the model
51
+ num_epochs=1
52
+ model.fit(train_generator, epochs=num_epochs)
53
+
54
+ # Evaluate the model on the test set
55
+ test_generator = test_datagen.flow_from_directory(
56
+ '/content/tire-dataset/test_data',
57
+ target_size=(224, 224),
58
+ batch_size=batch_size,
59
+ class_mode='categorical'
60
+ )
61
+
62
+ accuracy = model.evaluate(test_generator)
63
+ print('Test accuracy:', accuracy)
64
+
65
+
66
+
67
+ from tensorflow import keras
68
+ from tensorflow.keras.preprocessing import image
69
+ from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
70
+ import numpy as np
71
+
72
+ # Load the model
73
+ #model = keras.models.load_model('path_to_your_model.h5')
74
+
75
+ # Load and preprocess an image for inference
76
+ img_path = '/content/tire-dataset/test_data/Tire/00000.jpg'
77
+ img = image.load_img(img_path, target_size=(224, 224))
78
+ x = image.img_to_array(img)
79
+ x = np.expand_dims(x, axis=0)
80
+ x = preprocess_input(x)
81
+
82
+ # Make a prediction
83
+ predictions = model.predict(x)
84
+
85
+ # Decode and display the prediction
86
+ # decoded_predictions = decode_predictions(predictions, top=3)[0]
87
+ # for label, description, score in decoded_predictions:
88
+ # print(f'{label}: {description} ({score:.2f})')
89
+
90
+
91
+ model.save('/content/model_keras/keras_model.h5')
92
+
93
+
94
+ !tensorflowjs_converter --input_format=keras --output_format=tfjs_graph_model --split_weights_by_layer --weight_shard_size_bytes=99999999 --quantize_float16=* /content/model_keras/keras_model.h5 ./model_tfjs
95
+