Код: Выделить всё
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
# re-size all the images to this
IMAGE_SIZE = [224, 224]
train_path = '/content/drive/MyDrive/My training dataset/dataset + annotation'
valid_path = '/content/drive/MyDrive/My training dataset/dataset with axis'
inception = InceptionV3(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
for layer in inception.layers:
layer.trainable = False
folders = glob('/content/drive/MyDrive/My training dataset/dataset + annotation/*')
x = Flatten()(inception.output)
prediction = Dense(len(folders), activation='softmax')(x)
# create a model object
model = Model(inputs=inception.input, outputs=prediction)
#model.summary()
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# Use the Image Data Generator to import the images from the dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/My training dataset/dataset + annotation',
target_size = (224, 224),
batch_size = 16,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/My training dataset/dataset with axis',
target_size = (224, 224),
batch_size = 16,
class_mode = 'categorical')
r = model.fit(training_set, validation_data=test_set, epochs=10, steps_per_epoch=len(training_set), validation_steps=len(test_set))
# train the model
r = model.fit(
training_set,
validation_data=test_set,
epochs=10,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
# loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')
# accuracy
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')
from google.colab import files
from IPython.display import Image
uploaded = files.upload()
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
Upload the image and preprocess it
img_path = '/content/aez.png'
img = load_img(img_path, target_size=(224, 224))
img_array = np.expand_dims(img, axis=0) / 255
Make the prediction
prediction = model.predict(img_array)
**Get the class with the highest probability**
predicted_class = np.argmax(prediction)
print('Predicted class:', predicted_class)
Я хочу изменить выводимый текст, но не могу понять знаю, как это сделать.
Я также вручную пометил все изображения в моем наборе данных с помощью LabelImg и сохранил аннотации в виде файла .txt (профессор попросил меня использовать формат txt). и я не знаю, как использовать его для повторного обучения модели.
Подробнее здесь: https://stackoverflow.com/questions/783 ... ogle-colab