Views
No views yet
1model = tf.keras.models.load_model('model.keras')
2prediction = model.predict(image)
3print(prediction)1train_ds, val_ds = image_dataset_from_directory(
2 dataset_directory,
3 validation_split=0.2,
4 subset="both",
5 seed=123,
6 image_size=(200, 200),
7 batch_size=32,
8 color_mode='grayscale'
9)
10
11data_augmentation = tf.keras.Sequential([
12 tf.keras.layers.RandomFlip('horizontal'),
13 tf.keras.layers.RandomRotation(0.2),
14 tf.keras.layers.RandomZoom(0.2),
15 tf.keras.layers.RandomContrast(0.2),
16 tf.keras.layers.RandomBrightness(0.2),
17 tf.keras.layers.GaussianNoise(0.1),
18])
19
20AUTOTUNE = tf.data.AUTOTUNE
21train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
22val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
23
24model = tf.keras.Sequential([
25 data_augmentation,
26 tf.keras.layers.Rescaling(1./255, input_shape=(200, 200, 1)),
27 tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
28 tf.keras.layers.MaxPooling2D(2, 2),
29 tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
30 tf.keras.layers.MaxPooling2D(2, 2),
31 tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
32 tf.keras.layers.MaxPooling2D(2, 2),
33 tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
34 tf.keras.layers.MaxPooling2D(2, 2),
35 tf.keras.layers.Flatten(),
36 tf.keras.layers.Dropout(0.5),
37 tf.keras.layers.Dense(512, activation='relu'),
38 tf.keras.layers.Dense(2, activation='softmax')
39])
40
41model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
42 loss='sparse_categorical_crossentropy',
43 metrics=['accuracy'])1┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
2┃ Layer (type) ┃ Output Shape ┃ Param # ┃
3┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
4│ sequential (Sequential) │ (None, 200, 200, 1) │ 0 │
5├─────────────────────────────────┼────────────────────────┼───────────────┤
6│ rescaling (Rescaling) │ (None, 200, 200, 1) │ 0 │
7├─────────────────────────────────┼────────────────────────┼───────────────┤
8│ conv2d (Conv2D) │ (None, 198, 198, 64) │ 640 │
9├─────────────────────────────────┼────────────────────────┼───────────────┤
10│ max_pooling2d (MaxPooling2D) │ (None, 99, 99, 64) │ 0 │
11├─────────────────────────────────┼────────────────────────┼───────────────┤
12│ conv2d_1 (Conv2D) │ (None, 97, 97, 128) │ 73,856 │
13├─────────────────────────────────┼────────────────────────┼───────────────┤
14│ max_pooling2d_1 (MaxPooling2D) │ (None, 48, 48, 128) │ 0 │
15├─────────────────────────────────┼────────────────────────┼───────────────┤
16│ conv2d_2 (Conv2D) │ (None, 46, 46, 256) │ 295,168 │
17├─────────────────────────────────┼────────────────────────┼───────────────┤
18│ max_pooling2d_2 (MaxPooling2D) │ (None, 23, 23, 256) │ 0 │
19├─────────────────────────────────┼────────────────────────┼───────────────┤
20│ conv2d_3 (Conv2D) │ (None, 21, 21, 256) │ 590,080 │
21├─────────────────────────────────┼────────────────────────┼───────────────┤
22│ max_pooling2d_3 (MaxPooling2D) │ (None, 10, 10, 256) │ 0 │
23├─────────────────────────────────┼────────────────────────┼───────────────┤
24│ flatten (Flatten) │ (None, 25600) │ 0 │
25├─────────────────────────────────┼────────────────────────┼───────────────┤
26│ dropout (Dropout) │ (None, 25600) │ 0 │
27├─────────────────────────────────┼────────────────────────┼───────────────┤
28│ dense (Dense) │ (None, 1024) │ 26,215,424 │
29├─────────────────────────────────┼────────────────────────┼───────────────┤
30│ dense_1 (Dense) │ (None, 2) │ 2,050 │
31└─────────────────────────────────┴────────────────────────┴───────────────┘
32Total params: 81,531,656 (311.02 MB)
33Trainable params: 27,177,218 (103.67 MB)
34Non-trainable params: 0 (0 MB)
35Optimizer params 54,354,438 (207.35 MB)
| precision | recall | f1-score | support | |
|---|---|---|---|---|
| one_one | 0.98 | 0.98 | 0.96 | 1637 |
| one_two | 0.98 | 0.98 | 0.98 | 1740 |
| accuracy | 0.96 | 3377 | ||
| macro avg | 0.98 | 0.98 | 0.98 | 3377 |
| weighted avg | 0.98 | 0.98 | 0.98 | 3377 |