def vgg16(): """ Build VGG16 :return: model - model VGG16 """ input_shape = (config.HEIGHT, config.WIDTH, config.CHANNELS) input_tensor = Input(shape=input_shape) base_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor) top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(utils.n_classes_r1(), activation='softmax')) model = Sequential() for l in base_model.layers: model.add(l) model.add(top_model) for layer in model.layers[:15]: layer.trainable = False return model
def alexNet(): """ Build an AlexNet :return: model - model alexNet """ input_shape = (config.HEIGHT, config.WIDTH, config.CHANNELS) model = Sequential() model.add( Conv2D(32, (3, 3), padding='same', input_shape=input_shape, activation='relu')) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Conv2D(64, (3, 3), padding='same', activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Conv2D(128, (3, 3), padding='same', activation='relu')) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.2)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(utils.n_classes_r1(), activation='softmax')) return model
def predict(path_model, path_img): """ Predict classes for images, show plot with information :param path_model: path to model :param path_img: path to image or images :return: """ n_images_predict, full_paths_images_predict, sign_class = get_info_for_predict( path_img) model = utils.loading_saved_model(path_model) data_predict_image = utils.load_data(full_paths_images_predict) predict_imgs_outputs = model.predict(data_predict_image) fig, axes = plt.subplots(nrows=n_images_predict, ncols=2, figsize=(8, n_images_predict * 2)) fig.subplots_adjust(left=0, right=1, bottom=0.2, top=0.95, hspace=0.1, wspace=0.01) plt.style.use(['classic']) fig.set_facecolor('w') for i_row in range(n_images_predict): df_class = pd.DataFrame(predict_imgs_outputs[i_row], index=range(utils.n_classes_r1())) df_class = df_class.sort_values(0, ascending=False).head(3) for i_col in range(2): ax = axes[i_row, i_col] if i_col == 0: ax.imshow(data_predict_image[i_row]) # ax.set_ylabel(names_images_predict[i_row]) ax.set_xticks([]) ax.set_yticks([]) else: utils.plot_add_icon( sign_class, df_class, ax, xlims=[0, 2], delta_w=0.28, zoom=0.5, ) if i_row == 0: ax.tick_params(labeltop=True, labelbottom=False, labelsize=10) elif i_row == n_images_predict - 1: ax.tick_params(bottom=True, labeltop=False, labelsize=10) else: ax.set_xticks([]) plt.tight_layout() plt.show()
def confusion_matrix(model, train_X, train_Y, path_name): """ Build confusion matrix :param model: model :param train_X: data for predict :param train_Y: label for predict :param path_name: path to name model :return: """ y_pred = model.predict_classes(train_X) test_y_classes = np.argmax(train_Y, axis=1) con_mat1 = metrics.confusion_matrix(y_true=test_y_classes, y_pred=y_pred) con_mat_norm1 = np.around(con_mat1.astype('float') / con_mat1.sum(axis=1)[:, np.newaxis], decimals=2) con_mat_df1 = pd.DataFrame(con_mat_norm1, index=range(utils.n_classes_r1()), columns=range(utils.n_classes_r1())) fig = plt.figure(figsize=(30, 30)) ax = sb.heatmap(con_mat_df1, square=True, annot=True, linecolor='silver', cmap=plt.cm.Blues, linewidths=0.75, cbar=False) ax.tick_params(labelright=True, left=True, bottom=True, labeltop=True, rotation=0, labelsize=16) plt.ylabel('True class number', fontsize=20) plt.xlabel('Obtained class number', fontsize=20) fig.savefig(path_name + '_confusion_matrix.png')
def xception(): """ Build Xception :return: model - model Xception """ input_shape = (config.HEIGHT, config.WIDTH, config.CHANNELS) base_model = Xception(weights='imagenet', include_top=False, input_tensor=Input(input_shape)) for layer in base_model.layers: layer.trainable = False model = Sequential() model.add(base_model) model.add(Flatten()) model.add(Dense(400, activation='relu')) model.add(Dense(utils.n_classes_r1(), activation='sigmoid')) return model
def lenet(): """ Build LeNet model :return: model - model leNet """ input_shape = (config.HEIGHT, config.WIDTH, config.CHANNELS) model = Sequential() model.add( Conv2D(filters=16, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dense(128, activation='relu')) model.add(Dense(utils.n_classes_r1(), activation='softmax')) return model