for layer in basemodel.layers: layer.trainable = False # compiling the model opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # training the nets H = model.fit(aug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS, validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS) # predictions on Test set predictions = model.predict(testX, batch_size=BS) # finding the index of the largest probability predictions = np.argmax(predictions, axis=1) # show a nicely formatted classification report print( classification_report(testY.argmax(axis=1), predictions, target_names=lb.classes_)) # saving the model model.save(args["model"], save_format="h5") # Plotting the training loss and accuaracy N = EPOCHS
# Compile Model model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy']) ''' train model ''' batch_size = 256 num_epochs = 20 # Train Model history = model.fit(trainX, trainY, batch_size=batch_size, epochs=num_epochs) #, callbacks=[checkpoint]) predY = model.predict(testX) y_pred = np.argmax(predY, axis=1) y_actual = np.argmax(testY, axis=1) #y_label= [labels[k] for k in y_pred] cm = confusion_matrix(y_actual, y_pred) print(cm) ''' confusion matrix ''' import itertools def plot_confusion_matrix(cm, target_names, title='Confusion matrix',
# for image in filenames: # #print('../working/test1'+image) # img = cv2.imread('../working/test1/'+image) # img = cv2.resize(img,(224,224)) # img = img/255.0 # cls = model.predict(img.reshape(1,224,224,3)).argmax() # if cls==0: # category.append('cat') # else: # category.append('dog') # file.append(image) img = cv2.imread('../working/test1/101.jpg') img = cv2.resize(img, (224, 224)) img = img / 255.0 model.predict(img.reshape(1, 224, 224, 3)).argmax() import matplotlib.pyplot as plt plt.imshow(img) import pandas as pd ser1 = pd.Series(data=file) df_submit = pd.DataFrame(ser1, columns=['filename']) df_submit['category'] = category df_submit.head(5)
opt = Adam(lr=lr, decay=lr / epochs) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train print("[INFO] training model...") H = model.fit(x=X_train, y=y_train, batch_size=batch_size, steps_per_epoch=len(X_train) // batch_size, validation_data=(X_test, y_test), validation_steps=len(X_test) // batch_size, epochs=epochs) # test print("[INFO] evaluating model...") predIdxs = model.predict(X_test, batch_size=batch_size) predIdxs = np.argmax(predIdxs, axis=1) print( classification_report(y_test.argmax(axis=1), predIdxs, target_names=lb.classes_)) # confusion matrix cm = confusion_matrix(y_test.argmax(axis=1), predIdxs) total = sum(sum(cm)) acc = (cm[0, 0] + cm[1, 1]) / total sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1]) specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1]) print(cm)