class AmazonKerasClassifier: def __init__(self): self.losses = [] self.classifier = Sequential() def add_conv_layer(self, img_size=(32, 32), img_channels=3): self.classifier.add(BatchNormalization(input_shape=(img_size, img_channels))) self.classifier.add(Conv2D(32, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(32, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add(Conv2D(64, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(64, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add(Conv2D(128, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(128, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add(Conv2D(256, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(256, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) def add_flatten_layer(self): self.classifier.add(Flatten()) def add_ann_layer(self, output_size): self.classifier.add(Dense(512, activation='relu')) self.classifier.add(BatchNormalization()) self.classifier.add(Dropout(0.5)) self.classifier.add(Dense(output_size, activation='sigmoid')) def _get_fbeta_score(self, classifier, X_valid, y_valid): p_valid = classifier.predict(X_valid) return fbeta_score(y_valid, np.array(p_valid) > 0.2, beta=2, average='samples') def train_model(self, x_train, y_train, learn_rate=0.001, epoch=5, batch_size=128, validation_split_size=0.2, train_callbacks=()): history = LossHistory() X_train, X_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=validation_split_size) opt = Adam(lr=learn_rate) self.classifier.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) # early stopping will auto-stop training process if model stops learning after 3 epochs earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto') self.classifier.fit(X_train, y_train, batch_size=batch_size, epochs=epoch, verbose=1, validation_data=(X_valid, y_valid), callbacks=[history, *train_callbacks, earlyStopping]) fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid) return [history.train_losses, history.val_losses, fbeta_score] def save_weights(self, weight_file_path): self.classifier.save_weights(weight_file_path) def load_weights(self, weight_file_path): self.classifier.load_weights(weight_file_path) def predict(self, x_test): predictions = self.classifier.predict(x_test) return predictions def map_predictions(self, predictions, labels_map, thresholds): """ Return the predictions mapped to their labels :param predictions: the predictions from the predict() method :param labels_map: the map :param thresholds: The threshold of each class to be considered as existing or not existing :return: the predictions list mapped to their labels """ predictions_labels = [] for prediction in predictions: labels = [labels_map[i] for i, value in enumerate(prediction) if value > thresholds[i]] predictions_labels.append(labels) return predictions_labels def close(self): backend.clear_session()
class AmazonKerasClassifier: def __init__(self): self.losses = [] self.classifier = Sequential() def add_conv_layer(self, img_size=(32, 32), img_channels=3): self.classifier.add( BatchNormalization(input_shape=(*img_size, img_channels))) self.classifier.add( Conv2D(32, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(32, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(64, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(64, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(128, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(128, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(256, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(256, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) def add_flatten_layer(self): self.classifier.add(Flatten()) def add_ann_layer(self, output_size): self.classifier.add(Dense(512, activation='relu')) self.classifier.add(BatchNormalization()) self.classifier.add(Dropout(0.5)) self.classifier.add(Dense(output_size, activation='sigmoid')) def _get_fbeta_score(self, classifier, X_valid, y_valid): p_valid = classifier.predict(X_valid) return fbeta_score(y_valid, np.array(p_valid) > 0.2, beta=2, average='samples') def train_model(self, x_train, y_train, learn_rate=0.001, epoch=5, batch_size=128, validation_split_size=0.2, train_callbacks=()): history = LossHistory() X_train, X_valid, y_train, y_valid = train_test_split( x_train, y_train, test_size=validation_split_size) opt = Nadam(lr=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) self.classifier.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) # early stopping will auto-stop training process if model stops learning after 3 epochs earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto') for i in range(epoch): self.classifier.fit( X_train, y_train, batch_size=batch_size, epochs=1, verbose=2, validation_data=(X_valid, y_valid), callbacks=[history, *train_callbacks, earlyStopping]) fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid) print('fbeta score: %s' % fbeta_score) return [history.train_losses, history.val_losses, fbeta_score] def save_weights(self, weight_file_path): self.classifier.save_weights(weight_file_path) def load_weights(self, weight_file_path): self.classifier.load_weights(weight_file_path) def predict(self, x_test): predictions = self.classifier.predict(x_test) return predictions def map_predictions(self, predictions, labels_map, thresholds): """ Return the predictions mapped to their labels :param predictions: the predictions from the predict() method :param labels_map: the map :param thresholds: The threshold of each class to be considered as existing or not existing :return: the predictions list mapped to their labels """ predictions_labels = [] for prediction in predictions: labels = [ labels_map[i] for i, value in enumerate(prediction) if value > thresholds[i] ] predictions_labels.append(labels) return predictions_labels def close(self): backend.clear_session()
class AmazonKerasClassifier: def __init__(self): self.losses = [] self.classifier = Sequential() self.x_vail = [] self.y_vail = [] self.train_filepath = '' self.train_img_filepath = '' self.valid_filepath = '' self.valid_img_filepath = '' self.test_img_filepath = '' self.test_addition_img_filepath = '' self.test_img_name_list = '' self.y_map = {} def setTrainFilePath(self, value): self.train_filepath = value def getTrainFilePath(self): return self.train_filepath def setValidFilePath(self, value): self.valid_filepath = value def getValidFilePath(self): return self.valid_filepath def setTrainImgFilePath(self, value): self.train_img_filepath = value def getTrainImgFilePath(self): return self.train_img_filepath def setValidImgFilePath(self, value): self.valid_img_filepath = value def getValidImgFilePath(self): return self.valid_img_filepath def setTestImgFilePath(self, value): self.test_img_filepath = value def getTestImgFilePath(self): return self.test_img_filepath def setTestAdditionImgFilePath(self, value): self.test_addition_img_filepath = value def getTestAdditionImgFilePath(self): return self.test_addition_img_filepath def getTestImgNameList(self): return self.test_img_name_list def getYMap(self): return self.y_map def vgg(self, type=16, bn=False, img_size=(224, 224), img_channels=3, output_size=1000): if type == 16 and bn == False: layer_list = vgg.vgg16(num_classes=output_size) elif type == 16 and bn == True: layer_list = vgg.vgg16_bn(num_classes=output_size) elif type == 11 and bn == False: layer_list = vgg.vgg11(num_classes=output_size) elif type == 11 and bn == True: layer_list = vgg.vgg11_bn(num_classes=output_size) elif type == 13 and bn == False: layer_list = vgg.vgg13(num_classes=output_size) elif type == 13 and bn == True: layer_list = vgg.vgg13_bn(num_classes=output_size) elif type == 19 and bn == False: layer_list = vgg.vgg19(num_classes=output_size) elif type == 19 and bn == True: layer_list = vgg.vgg19_bn(num_classes=output_size) else: print("请输入11,13,16,19这四个数字中的一个!") self.classifier.add( BatchNormalization(input_shape=(*img_size, img_channels))) for i, value in enumerate(layer_list): self.classifier.add(eval(value)) def squeezenet(self, type, img_size=(64, 64), img_channels=3, output_size=1000): input_shape = Input(shape=(*img_size, img_channels)) if type == 1: x = squeezenet.squeezenet1_0(input_shape, num_classes=output_size) elif type == 1.1: x = squeezenet.squeezenet1_1(input_shape, num_classes=output_size) else: print("请输入1,1.0这两个数字中的一个!") model = Model(inputs=input_shape, outputs=x) self.classifier = model def resnet(self, type, img_size=(64, 64), img_channels=3, output_size=1000): input_shape = Input(shape=(*img_size, img_channels)) if type == 18: x = resnet.resnet18(input_shape, num_classes=output_size) elif type == 34: x = resnet.resnet34(input_shape, num_classes=output_size) elif type == 50: x = resnet.resnet50(input_shape, num_classes=output_size) elif type == 101: x = resnet.resnet101(input_shape, num_classes=output_size) elif type == 152: x = resnet.resnet152(input_shape, num_classes=output_size) else: print("请输入18,34,50,101,152这五个数字中的一个!") return model = Model(inputs=input_shape, outputs=x) self.classifier = model def inception(self, img_size=(299, 299), img_channels=3, output_size=1000): input_shape = Input(shape=(*img_size, img_channels)) x = inception.inception_v3(input_shape, num_classes=output_size, aux_logits=True, transform_input=False) model = Model(inputs=input_shape, outputs=x) self.classifier = model def densenet(self, type, img_size=(299, 299), img_channels=3, output_size=1000): input_shape = Input(shape=(*img_size, img_channels)) if type == 161: x = densenet.densenet161(input_shape, num_classes=output_size) elif type == 121: x = densenet.densenet121(input_shape, num_classes=output_size) elif type == 169: x = densenet.densenet169(input_shape, num_classes=output_size) elif type == 201: x = densenet.densenet201(input_shape, num_classes=output_size) else: print("请输入161,121,169,201这四个数字中的一个!") return model = Model(inputs=input_shape, outputs=x) self.classifier = model def alexnet(self, img_size=(299, 299), img_channels=3, output_size=1000): input_shape = Input(shape=(*img_size, img_channels)) x = alexnet.alexnet(input_shape, num_classes=output_size) model = Model(inputs=input_shape, outputs=x) self.classifier = model def add_conv_layer(self, img_size=(32, 32), img_channels=3): self.classifier.add( BatchNormalization(input_shape=(*img_size, img_channels))) self.classifier.add( Conv2D(32, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(32, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(64, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(64, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(128, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(128, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) self.classifier.add( Conv2D(256, (3, 3), padding='same', activation='relu')) self.classifier.add(Conv2D(256, (3, 3), activation='relu')) self.classifier.add(MaxPooling2D(pool_size=2)) self.classifier.add(Dropout(0.25)) def add_flatten_layer(self): self.classifier.add(Flatten()) def add_ann_layer(self, output_size): self.classifier.add(Dense(512, activation='relu')) self.classifier.add(BatchNormalization()) self.classifier.add(Dropout(0.5)) self.classifier.add(Dense(output_size, activation='sigmoid')) def _get_fbeta_score2(self, classifier, X_valid, y_valid): p_valid = classifier.predict(X_valid) result_threshold_list_final, score_result = self.grid_search_best_threshold( y_valid, np.array(p_valid)) return result_threshold_list_final, score_result def _get_fbeta_score(self, classifier, X_valid, y_valid): p_valid = classifier.predict(X_valid) return fbeta_score(y_valid, np.array(p_valid) > 0.2, beta=2, average='samples') def grid_search_best_threshold(self, y_valid, p_valid): threshold_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] result_threshold_list_temp = [ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 ] result_threshold_list_final = [ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 ] for i in range(17): score_result = 0 for j in range(9): result_threshold_list_temp[i] = threshold_list[j] score_temp = fbeta_score(y_valid, p_valid > result_threshold_list_temp, beta=2, average='samples') if score_result < score_temp: score_result = score_temp result_threshold_list_final[i] = threshold_list[j] result_threshold_list_temp[i] = result_threshold_list_final[i] return result_threshold_list_final, score_result def train_model(self, x_train, y_train, learn_rate=0.001, epoch=5, batch_size=128, validation_split_size=0.2, train_callbacks=()): history = LossHistory() X_train, X_valid, y_train, y_valid = train_test_split( x_train, y_train, test_size=validation_split_size) self.x_vail = X_valid self.y_vail = y_valid opt = Adam(lr=learn_rate) self.classifier.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto') self.classifier.fit( X_train, y_train, batch_size=batch_size, epochs=epoch, verbose=1, validation_data=(X_valid, y_valid), callbacks=[history, *train_callbacks, earlyStopping]) fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid) return [history.train_losses, history.val_losses, fbeta_score] def train_model_generator(self, generator_train, generator_valid, learn_rate=0.001, epoch=5, batchSize=128, steps=32383, validation_steps=8096, train_callbacks=()): history = LossHistory() #valid 8096 32383 opt = Adam(lr=learn_rate) steps = steps / batchSize + 1 - 9 validation_steps = validation_steps / batchSize + 1 if steps % batchSize == 0: steps = steps / batchSize - 9 if validation_steps % batchSize == 0: validation_steps = validation_steps / batchSize print(steps, validation_steps) self.classifier.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto') self.classifier.fit_generator( generator_train, steps_per_epoch=steps, epochs=epoch, verbose=1, validation_data=generator_valid, validation_steps=validation_steps, callbacks=[history, *train_callbacks, earlyStopping]) fbeta_score = self._get_fbeta_score(self.classifier, X_valid, y_valid) return [history.train_losses, history.val_losses, fbeta_score] def generate_trainOrValid_img_from_file(self, train_set_folder, train_csv_file, img_resize=(32, 32), batchSize=128, process_count=cpu_count()): labels_df = pd.read_csv(train_csv_file) labels = sorted( set( chain.from_iterable( [tags.split(" ") for tags in labels_df['tags'].values]))) labels_map = {l: i for i, l in enumerate(labels)} files_path = [] tags_list = [] for file_name, tags in labels_df.values: files_path.append('{}/{}.jpg'.format(train_set_folder, file_name)) tags_list.append(tags) X = [] Y = [] iter_num = 1 self.y_map = {v: k for k, v in labels_map.items()} with ThreadPoolExecutor(process_count) as pool: for img_array, targets in tqdm(pool.map( self._train_transform_to_matrices, [(file_path, tag, labels_map, img_resize) for file_path, tag in zip(files_path, tags_list)]), total=len(files_path)): if iter_num % batchSize == 0: X = [] Y = [] iter_num = 0 X.append(img_array) Y.append(targets) iter_num += 1 if iter_num == batchSize: print(iter_num) yield (np.array(X), np.array(Y)) def _train_transform_to_matrices(self, *args): file_path, tags, labels_map, img_resize = list(args[0]) img = Image.open(file_path) img.thumbnail(img_resize) img_array = np.asarray(img.convert("RGB"), dtype=np.float32) / 255 targets = np.zeros(len(labels_map)) for t in tags.split(' '): targets[labels_map[t]] = 1 return img_array, targets def generate_test_img_from_file(self, test_set_folder, img_resize=(32, 32), batchSize=128, process_count=cpu_count()): x_test = [] x_test_filename = [] files_name = os.listdir(test_set_folder) X = [] Y = [] iter_num = 1 with ThreadPoolExecutor(process_count) as pool: for img_array, file_name in tqdm(pool.map( _test_transform_to_matrices, [(test_set_folder, file_name, img_resize) for file_name in files_name]), total=len(files_name)): x_test.append(img_array) x_test_filename.append(file_name) self.test_img_name_list = x_test_filename if iter_num % batchSize == 0: X = [] Y = [] iter_num = 0 X.append(img_array) Y.append(targets) iter_num += 1 if iter_num == batchSize: print(iter_num) yield (np.array(X), np.array(Y)) def _test_transform_to_matrices(self, *args): test_set_folder, file_name, img_resize = list(args[0]) img = Image.open('{}/{}'.format(test_set_folder, file_name)) img.thumbnail(img_resize) # Convert to RGB and normalize img_array = np.array(img.convert("RGB"), dtype=np.float32) / 255 return img_array, file_name def save_weights(self, weight_file_path): self.classifier.save_weights(weight_file_path) def load_weights(self, weight_file_path): self.classifier.load_weights(weight_file_path) def setBestThreshold(self): result_threshold_list_final, score_result = self._get_fbeta_score2( self.classifier, self.x_vail, self.y_vail) print('最好得分:{}'.format(score_result)) print('最好的阈值:{}'.format(result_threshold_list_final)) return result_threshold_list_final def predict(self, x_test): predictions = self.classifier.predict(x_test) return predictions def predict_generator(self, generator): predictions = self.classifier.predcit_generator(generator) return predictions def map_predictions(self, predictions, labels_map, thresholds): predictions_labels = [] for prediction in predictions: labels = [ labels_map[i] for i, value in enumerate(prediction) if value > thresholds[i] ] predictions_labels.append(labels) return predictions_labels def close(self): backend.clear_session()
class KerasModel: def __init__(self, img_size, img_channels=3, output_size=17): self.losses = [] self.model = Sequential() self.model.add( BatchNormalization(input_shape=(img_size[0], img_size[1], img_channels))) self.model.add(Conv2D(32, (3, 3), padding='same', activation='relu')) self.model.add(Conv2D(32, (3, 3), activation='relu')) self.model.add(MaxPooling2D(pool_size=2)) self.model.add(Dropout(0.3)) self.model.add(Conv2D(64, (3, 3), padding='same', activation='relu')) self.model.add(Conv2D(64, (3, 3), activation='relu')) self.model.add(MaxPooling2D(pool_size=2)) self.model.add(Dropout(0.3)) self.model.add(Conv2D(128, (3, 3), padding='same', activation='relu')) self.model.add(Conv2D(128, (3, 3), activation='relu')) self.model.add(MaxPooling2D(pool_size=2)) self.model.add(Dropout(0.3)) self.model.add(Conv2D(256, (3, 3), padding='same', activation='relu')) self.model.add(Conv2D(256, (3, 3), activation='relu')) self.model.add(MaxPooling2D(pool_size=2)) self.model.add(Dropout(0.3)) self.model.add(Conv2D(512, (3, 3), padding='same', activation='relu')) self.model.add(Conv2D(512, (3, 3), activation='relu')) self.model.add(MaxPooling2D(pool_size=2)) self.model.add(Dropout(0.3)) self.model.add(Flatten()) self.model.add(Dense(512, activation='relu')) self.model.add(BatchNormalization()) self.model.add(Dropout(0.5)) self.model.add(Dense(output_size, activation='sigmoid')) def get_fbeta_score(self, validation_data, verbose=True): p_valid = self.model.predict(validation_data[0]) thresholds = optimise_f2_thresholds(validation_data[1], p_valid, verbose=verbose) return fbeta_score(validation_data[1], np.array(p_valid) > thresholds, beta=2, average='samples'), thresholds def fit(self, flow, epochs, lr, validation_data, train_callbacks=[], batches=300): history = LossHistory() fbeta = Fbeta(validation_data) opt = Adam(lr=lr) self.model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) earlyStopping = EarlyStopping(monitor='val_loss', patience=3, verbose=0, mode='auto') self.model.fit_generator(flow, steps_per_epoch=batches, epochs=epochs, callbacks=[history, earlyStopping, fbeta] + train_callbacks, validation_data=validation_data) fb_score, thresholds = self.get_fbeta_score(validation_data, verbose=False) return [ fbeta.fbeta, history.train_losses, history.val_losses, fb_score, thresholds ] def save_weights(self, weight_file_path): self.model.save_weights(weight_file_path) def load_weights(self, weight_file_path): self.model.load_weights(weight_file_path) def predict_image(self, image): img = Image.fromarray(np.uint8(image * 255)) images = [img.copy().rotate(i) for i in [-90, 90, 180]] images.append(img) images = np.asarray([ np.asarray(image.convert("RGB"), dtype=np.float32) / 255 for image in images ]) return sum(self.model.predict(images)) / 4 def predict(self, x_test): return [self.predict_image(img) for img in tqdm(x_test)] def map_predictions(self, predictions, labels_map, thresholds): predictions_labels = [] for prediction in predictions: labels = [ labels_map[i] for i, value in enumerate(prediction) if value > thresholds[i] ] predictions_labels.append(labels) return predictions_labels def close(self): backend.clear_session()
"""Predict if the customer with the following informations will leave the bank: Geography: France Credit Score: 600 Gender: Male Age: 40 Tenure: 3 Balance: 60000 Number of Products: 2 Has Credit Card: Yes Is Active Member: Yes Estimated Salary: 50000""" new_prediction = classifier.predict( sc.transform(np.array([[0.0, 0, 600, 1, 40, 3, 60000, 2, 1, 1, 50000]]))) new_prediction = (new_prediction > 0.5) # Making the Confusion Matrix from sklearn.metrics import confusion_matrix #confusion matric which consists of diagonal of elements of right and wrong predictions cm = confusion_matrix(y_test, y_pred) print(cm) backend.clear_session() #Saving the weights fname = "weights-ann.hdf5" classifier.save_weights(fname, overwrite=True) #loading the weights fname = "weights-ann.hdf5" classifier.load_weights(fname)
class_mode='binary') # Create a loss history history = LossHistory() classifier.fit_generator(training_set, steps_per_epoch=8000 / batch_size, epochs=90, validation_data=test_set, validation_steps=2000 / batch_size, workers=12, max_q_size=100, callbacks=[history]) # serialize model to JSON model_json = classifier.to_json() with open("catordog.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 classifier.save_weights("catordog.h5") print("Saved model to disk") # Save loss history to file loss_history_path = os.path.join(script_dir, 'loss_history.log') myFile = open(loss_history_path, 'w+') myFile.write(history.losses) myFile.close() backend.clear_session() print("The model class indices are:", training_set.class_indices)
class Model(object): def __init__(self): self.model = Sequential() self.model.add( Conv2D(32, (3, 3), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Conv2D(32, (3, 3))) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Conv2D(64, (3, 3))) self.model.add(Activation('relu')) self.model.add(MaxPooling2D(pool_size=(2, 2))) self.model.add(Flatten()) self.model.add(Dense(64)) self.model.add(Activation('relu')) self.model.add(Dropout(0.5)) self.model.add(Dense(1)) self.model.add(Activation('sigmoid')) def train(self, dataset, batch_size=batch_size, nb_epoch=epochs): self.model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) self.model.fit_generator( dataset.train, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=dataset.valid, validation_steps=nb_validation_samples // batch_size) def save(self, file_path=FILE_PATH): print('Model Saved.') self.model.save_weights(file_path) def load(self, file_path=FILE_PATH): print('Model Loaded.') self.model.load_weights(file_path) def predict(self, image): # 预测样本分类 img = image.resize((1, IMAGE_SIZE, IMAGE_SIZE, 3)) img = image.astype('float32') img /= 255 #归一化 result = self.model.predict(img) print(result) # 概率 result = self.model.predict_classes(img) print(result) # 0/1 return result[0] def evaluate(self, dataset): # 测试样本准确率 score = self.model.evaluate_generator(dataset.valid, steps=2) print("样本准确率%s: %.2f%%" % (self.model.metrics_names[1], score[1] * 100))
class TinyYoloV2: """ This class handles the building and the loss of the tiny yolo v2 network. """ def __init__(self, config): """ Initializes class variables. :param config: Contains the networks hyperparameters """ #super.__init__(self) self.config = config self.network = None self.loss = None self.input_shape = config.input_shape def build(self): """ Builds the tiny yolo v2 network. :param input: input image batch to the network :return: logits output from network """ self.model = Sequential() self.model.add(Convolution2D(16, (3, 3), input_shape=(416, 416, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) self.model.add(Convolution2D(32, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) self.model.add(Convolution2D(64, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) self.model.add(Convolution2D(128, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) self.model.add(Convolution2D(256, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) self.model.add(Convolution2D(512, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 1), padding='valid')) self.model.add(Convolution2D(1024, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(Convolution2D(1024, (3, 3), padding='same')) self.model.add(LeakyReLU()) self.model.add(BatchNormalization()) self.model.add(Convolution2D(125, (1, 1), activation=None)) if self.config.optimizer == 'adam': opt = Adam() elif self.config.optimizer == 'sgd': opt = SGD() if self.config.loss == 'categorical_crossentropy': loss = 'categorical_crossentropy' elif self.config.loss == 'yolov2_loss': raise NotImplemented self.model.compile(loss=loss, optimizer=opt, metrics=['accuracy']) self.model.summary() return self.model def convertToBoxParams(self, out): """ Convert the final layer features to bounding box parameters. :return: box_xy: tensor x, y box predictions adjusted by spatial location in conv layer box_wh: tensor w, h box predictions adjusted by anchors and conv spatial resolution box_conf: tensor Probability estimate for whether each box contains any object box_class_pred : tensor Probability distribution estimate for each box over class labels """ feats = out num_anchors = len(self.config.anchors) # Reshape to batch, height, width, num_anchors, box_params anchors_tensor = K.reshape(K.variable(self.config.anchors), [1, 1, 1, num_anchors, 2]) conv_dims = K.shape(feats)[1:3] # assuming channels last # In YOLO the height index is the inner most iteration conv_height_index = K.arange(0, stop=conv_dims[0]) conv_width_index = K.arange(0, stop=conv_dims[1]) conv_height_index = K.tile(conv_height_index, [conv_dims[1]]) conv_width_index = K.tile( K.expand_dims(conv_width_index, 0), [conv_dims[0], 1]) conv_width_index = K.flatten(K.transpose(conv_width_index)) conv_index = K.transpose(K.stack([conv_height_index, conv_width_index])) conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2]) conv_index = K.cast(conv_index, K.dtype(feats)) feats = K.reshape( feats, [-1, conv_dims[0], conv_dims[1], num_anchors, self.config.classes + 5]) conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats)) # Static generation of conv_index: # conv_index = np.array([_ for _ in np.ndindex(conv_width, conv_height)]) # conv_index = conv_index[:, [1, 0]] # swap columns for YOLO ordering. # conv_index = K.variable( # conv_index.reshape(1, conv_height, conv_width, 1, 2)) # feats = Reshape( # (conv_dims[0], conv_dims[1], num_anchors, num_classes + 5))(feats) box_xy = K.sigmoid(feats[..., :2]) box_wh = K.exp(feats[..., 2:4]) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.softmax(feats[..., 5:]) # Adjust preditions to each spatial grid point and anchor size. # Note: YOLO iterates over height index before width index. box_xy = (box_xy + conv_index) / conv_dims box_wh = box_wh * anchors_tensor / conv_dims return box_xy, box_wh, box_confidence, box_class_probs def save_model(self, name): """ Saves model to path. :return: """ path = "C:\ObjectDetection\Data\ModelCheckpoints" + name + ".h5" self.model.save(path) def save_weights(self, name): """ Saves the model weights to path. :param name: :return: """ path = "C:\ObjectDetection\Data\ModelCheckpoints" + name + ".h5" self.model.save_weights(path) def load_weights(self): print("About to load weights.") utils.load_weights(self.model, 'C:\\ObjectDetection\\Data\\ModelCheckpoints\\tiny-yolo-voc.weights') print("Loaded weights.") def _load_pretrained_network(self): """ Loads the pretrained network's weights into the new network. :return: """ raise NotImplemented