In [ ]:
# crear conjunto de datos
In [6]:
!unzip /content/Ventiladores/ventiladores.zip > /content/dataset
replace facebook_sharing_5.png.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
In [7]:
! sudo mv *.png *.jpg *.jpeg /content/Ventiladores
In [ ]:
! mv /content/Ventiladores & /content/Computadoras > /content/dataset
In [ ]:
! sudo mv babuino/ dataset/babuino
In [ ]:
! sudo mv jirafa/ dataset/jirafa
mv: cannot stat 'jirafa/': No such file or directory
In [10]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
In [11]:
import numpy as np
In [ ]:
# crear dataset generador
In [12]:
datagen= ImageDataGenerator(
# normalizacion de las imagenes
rescale=1. / 255,
#rotacion
rotation_range = 10,
# giro de la imagen
width_shift_range=0.15,
# movimiento hacia arriba
height_shift_range = 0.1,
# deformacion de las imagenes
shear_range = 5,
# acercamiento
zoom_range = [0.7 , 1.3],
# validacion -> porcentaje de las imagenes a ser utilziadas en las pruebas
validation_split= 0.2,
)
In [13]:
# datos entrenamiento y datos de prueba
data_gen_entrenamiento = datagen.flow_from_directory("/content/dataset",
target_size=(224,224),
batch_size=32 , shuffle=True,
subset="training"
)
Found 101 images belonging to 2 classes.
In [14]:
data_gen_pruebas = datagen.flow_from_directory("/content/dataset",
target_size=(224,224),
batch_size=32 , shuffle=True,
subset="validation"
)
Found 24 images belonging to 2 classes.
In [15]:
import matplotlib.pyplot as plt
for imagenes , etiqueta in data_gen_entrenamiento:
for i in range(10):
plt.subplot(2,5,i+1)
plt.imshow(imagenes[1])
break
plt.show()
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
In [25]:
import tensorflow as tf
import tensorflow_hub as hub
url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
mobilenetv2 = hub.KerasLayer(url , input_shape=(224,224,3))
In [26]:
# importante -> congelar las capas
# evitar el sobre entrenamiento del modelo descargado de google
mobilenetv2.trainable = False
In [28]:
# creacion del modelo en base al de google
modelo = tf.keras.Sequential([
mobilenetv2,
tf.keras.layers.Dense(units=100),
tf.keras.layers.Dense(units=100),
tf.keras.layers.Dense(2 , activation="softmax")
])
In [29]:
modelo.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
In [30]:
EPOCAS = 20
entrenamiento = modelo.fit(
data_gen_entrenamiento,
epochs=EPOCAS,
batch_size = 32,
validation_data = data_gen_pruebas
)
Epoch 1/20
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 1.4060 - accuracy: 0.6337
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn( /usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 7s 682ms/step - loss: 1.4060 - accuracy: 0.6337 - val_loss: 1.1179 - val_accuracy: 0.5000 Epoch 2/20 1/4 [======>.......................] - ETA: 1s - loss: 1.0500 - accuracy: 0.5625
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 595ms/step - loss: 1.0861 - accuracy: 0.5842 - val_loss: 0.9222 - val_accuracy: 0.5833 Epoch 3/20
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
2/4 [==============>...............] - ETA: 0s - loss: 0.3390 - accuracy: 0.8919
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 601ms/step - loss: 0.4899 - accuracy: 0.8218 - val_loss: 1.1148 - val_accuracy: 0.6667 Epoch 4/20 1/4 [======>.......................] - ETA: 0s - loss: 0.0131 - accuracy: 1.0000
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
2/4 [==============>...............] - ETA: 0s - loss: 0.4684 - accuracy: 0.8378
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 493ms/step - loss: 0.5889 - accuracy: 0.8119 - val_loss: 0.7661 - val_accuracy: 0.5833 Epoch 5/20 2/4 [==============>...............] - ETA: 0s - loss: 0.0930 - accuracy: 0.9730
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 546ms/step - loss: 0.2613 - accuracy: 0.8812 - val_loss: 1.0604 - val_accuracy: 0.5000
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
Epoch 6/20 2/4 [==============>...............] - ETA: 1s - loss: 0.4185 - accuracy: 0.7969
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.4010 - accuracy: 0.8218
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 451ms/step - loss: 0.4010 - accuracy: 0.8218 - val_loss: 0.7535 - val_accuracy: 0.5833 Epoch 7/20 3/4 [=====================>........] - ETA: 0s - loss: 0.2684 - accuracy: 0.8841
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.3426 - accuracy: 0.8416
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 3s 635ms/step - loss: 0.3426 - accuracy: 0.8416 - val_loss: 0.7650 - val_accuracy: 0.6667 Epoch 8/20 2/4 [==============>...............] - ETA: 1s - loss: 0.3247 - accuracy: 0.8750
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn( /usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.3437 - accuracy: 0.8713
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 4s 939ms/step - loss: 0.3437 - accuracy: 0.8713 - val_loss: 0.7574 - val_accuracy: 0.7083 Epoch 9/20 4/4 [==============================] - ETA: 0s - loss: 0.2633 - accuracy: 0.8614
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 3s 611ms/step - loss: 0.2633 - accuracy: 0.8614 - val_loss: 0.9748 - val_accuracy: 0.5833
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
Epoch 10/20 2/4 [==============>...............] - ETA: 0s - loss: 0.4512 - accuracy: 0.8108
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 481ms/step - loss: 0.3047 - accuracy: 0.8515 - val_loss: 0.6240 - val_accuracy: 0.7083 Epoch 11/20 2/4 [==============>...............] - ETA: 1s - loss: 0.1430 - accuracy: 0.9375
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 3s 663ms/step - loss: 0.2041 - accuracy: 0.8911 - val_loss: 0.7710 - val_accuracy: 0.6667 Epoch 12/20 1/4 [======>.......................] - ETA: 1s - loss: 0.2757 - accuracy: 0.8438
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn( /usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 379ms/step - loss: 0.2566 - accuracy: 0.8713 - val_loss: 0.7065 - val_accuracy: 0.6667 Epoch 13/20 3/4 [=====================>........] - ETA: 0s - loss: 0.1696 - accuracy: 0.9275
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.2146 - accuracy: 0.9109
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 463ms/step - loss: 0.2146 - accuracy: 0.9109 - val_loss: 0.7602 - val_accuracy: 0.5417 Epoch 14/20 4/4 [==============================] - 2s 451ms/step - loss: 0.2082 - accuracy: 0.9109 - val_loss: 0.7276 - val_accuracy: 0.7083 Epoch 15/20
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
3/4 [=====================>........] - ETA: 0s - loss: 0.1590 - accuracy: 0.9167
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn( /usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.1603 - accuracy: 0.9109
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 441ms/step - loss: 0.1603 - accuracy: 0.9109 - val_loss: 0.8428 - val_accuracy: 0.6667 Epoch 16/20 4/4 [==============================] - 2s 564ms/step - loss: 0.2661 - accuracy: 0.8713 - val_loss: 0.8954 - val_accuracy: 0.6250
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
Epoch 17/20
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.2476 - accuracy: 0.8812
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn( /usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 3s 792ms/step - loss: 0.2476 - accuracy: 0.8812 - val_loss: 0.8608 - val_accuracy: 0.5417 Epoch 18/20 1/4 [======>.......................] - ETA: 1s - loss: 0.2128 - accuracy: 0.8750
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 554ms/step - loss: 0.3182 - accuracy: 0.8614 - val_loss: 0.9143 - val_accuracy: 0.6250 Epoch 19/20 1/4 [======>.......................] - ETA: 0s - loss: 0.2088 - accuracy: 0.8000
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
3/4 [=====================>........] - ETA: 0s - loss: 0.2257 - accuracy: 0.8841
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 572ms/step - loss: 0.2089 - accuracy: 0.9010 - val_loss: 0.7905 - val_accuracy: 0.6250 Epoch 20/20 3/4 [=====================>........] - ETA: 0s - loss: 0.1996 - accuracy: 0.9275
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - ETA: 0s - loss: 0.2304 - accuracy: 0.9010
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:996: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images warnings.warn(
4/4 [==============================] - 2s 421ms/step - loss: 0.2304 - accuracy: 0.9010 - val_loss: 0.8490 - val_accuracy: 0.6250
In [34]:
from PIL import Image
import cv2
def categorizar(ruta):
img = Image.open(ruta)
img = img.convert("RGB")
img = np.array(img).astype(float)/255
img = cv2.resize(img , (224,224))
prediccion = modelo.predict(img.reshape(-1, 224,224,3))
return np.argmax(prediccion[0] , axis=-1)
In [35]:
ruta = input("indique donde se encuentra el archivo de imagen : \n" )
prediccion = categorizar(ruta)
if prediccion == 1:
print("La imagen analizada corresponde a una Jirafa")
elif prediccion == 0:
print("La imagen analizada corresponde a un babuino")
else:
print("el archivo no se encontró")
indique donde se encuentra el archivo de imagen : ventilador.jpg 1/1 [==============================] - 0s 454ms/step