martedì 26 novembre 2019

Classificazione binaria foraminferi con Tensorflow

Per continuare gli esperimenti questa una classificazione binaria con Tensorflow.
Le due categorie sono riprese dal precedente post (1500 elementi)


Globigerinoides Ruber
Globigerina Bulloides

train
======================================================
import os


a_dir = os.path.join('./train/globigerinoides_ruber')
b_dir = os.path.join('./train/globigerina_bulloides')


print('globigerinoides_ruber images:', len(os.listdir(a_dir)))
print('globigerina_bulloides images:', len(os.listdir(b_dir)))

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

batch_size = 16


from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1/255)

train_generator = train_datagen.flow_from_directory(
        './train_binary',  
        target_size=(200, 200),  
        batch_size=batch_size,
shuffle=True,
        class_mode='binary')

import tensorflow as tf

model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(200, 200 ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


model.summary()

total_sample=train_generator.n

n_epochs = 15
history = model.fit_generator(
        train_generator, 
        steps_per_epoch=int(total_sample/batch_size),  
        epochs=n_epochs,
        verbose=1)

model.save('1500el_5classi_binary_model.h5')

======================================================

predict
======================================================
import tensorflow as tf
from tensorflow.keras import Model 
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import sys
from numpy import asarray
import numpy as np

model = tf.compat.v2.keras.models.load_model('1500el_5classi_binary_model.h5')



image = Image.open(sys.argv[1])
image = image.resize((200,200))
pic = asarray(image)
pic = pic.astype('float32')
pic /= 255.0

pic=np.expand_dims(pic,axis=0)


prediction = model.predict(pic)
print(sys.argv[1])

float_formatter = "{:.2f}".format
np.set_printoptions(formatter={'float_kind':float_formatter})
print(prediction)


print()
======================================================

I risultati indicano 19 tentativi 13 corrette identificazioni (69%), 2 casi incerti (10.5%), 2 errori  (10.5%)


Globigerinoides Ruber Globigerina Bulloides


Predizione
Immagini reali



Globigerina Bulloides 1 0% 100%

2 100% 0%

3 0% 100%

4 0% 100%

5 0% 100%

6 58% 42%

7 47% 53%

8 0% 100%

9 0% 100%
Globigerinoides Ruber 1 100% 0%

2 0% 100%

3 63% 37%

4 100% 0%

5 100% 0%

6 94% 6%

7 0% 100%

8 0% 100%

9 100% 0%

10 100% 0%

Nessun commento:

Posta un commento

Debugger integrato ESP32S3

Aggiornamento In realta' il Jtag USB funziona anche sui moduli cinesi Il problema risiede  nell'ID USB della porta Jtag. Nel modulo...