def train_autokeras(self): #Load images train_data, train_labels = load_image_dataset(csv_file_path=self.TRAIN_CSV_DIR, images_path=self.RESIZE_TRAIN_IMG_DIR) test_data, test_labels = load_image_dataset(csv_file_path=self.TEST_CSV_DIR, images_path=self.RESIZE_TEST_IMG_DIR) train_data = train_data.astype('float32') / 255 test_data = test_data.astype('float32') / 255 print("Train data shape:", train_data.shape) clf = ImageClassifier(verbose=True, path=self.TEMP_DIR, resume=False) clf.fit(train_data, train_labels, time_limit=self.TIME) clf.final_fit(train_data, train_labels, test_data, test_labels, retrain=True) evaluate_value = clf.evaluate(test_data, test_labels) print("Evaluate:", evaluate_value) # clf.load_searcher().load_best_model().produce_keras_model().save(MODEL_DIR) # clf.export_keras_model(MODEL_DIR) clf.export_autokeras_model(self.MODEL_DIR) #统计训练信息 dic = {} ishape = clf.cnn.searcher.input_shape dic['n_train'] = train_data.shape[0] #训练总共用了多少图 dic['n_classes'] = clf.cnn.searcher.n_classes dic['input_shape'] = str(ishape[0]) + 'x' + str(ishape[1]) + 'x' + str(ishape[2]) dic['history'] = clf.cnn.searcher.history dic['model_count'] = clf.cnn.searcher.model_count dic['best_model'] = clf.cnn.searcher.get_best_model_id() best_model = [item for item in dic['history'] if item['model_id'] == dic['best_model']] if len(best_model) > 0: dic['loss'] = best_model[0]['loss'] dic['metric_value'] = best_model[0]['metric_value'] dic['evaluate_value'] = evaluate_value self.traininfo = dic
def train_autokeras(RESIZE_TRAIN_IMG_DIR, RESIZE_TEST_IMG_DIR, TRAIN_CSV_DIR, TEST_CSV_DIR, TIME): # Load images train_data, train_labels = load_image_dataset( csv_file_path=TRAIN_CSV_DIR, images_path=RESIZE_TRAIN_IMG_DIR) # 加载数据 test_data, test_labels = load_image_dataset( csv_file_path=TEST_CSV_DIR, images_path=RESIZE_TEST_IMG_DIR) train_data = train_data.astype('float32') / 255 test_data = test_data.astype('float32') / 255 clf = ImageClassifier(verbose=True) clf.fit(train_data, train_labels, time_limit=TIME) # 找最优模型 clf.final_fit(train_data, train_labels, test_data, test_labels, retrain=True) # 最优模型继续训练 y = clf.evaluate(test_data, test_labels) print("测试集精确度:", y) score = clf.evaluate(train_data, train_labels) # score: 0.8139240506329114 print("训练集精确度:", score) clf.export_keras_model(MODEL_DIR) # 储存
def train_autokeras(RESIZE_TRAIN_IMG_DIR, TRAIN_CSV_DIR, RESIZE_TEST_IMG_DIR, TEST_CSV_DIR, TIME): #Load images train_data, train_labels = load_image_dataset( csv_file_path=TRAIN_CSV_DIR, images_path=RESIZE_TRAIN_IMG_DIR) test_data, test_labels = load_image_dataset( csv_file_path=TEST_CSV_DIR, images_path=RESIZE_TEST_IMG_DIR) train_data = train_data.astype('float32') / 255 test_data = test_data.astype('float32') / 255 print("Train data shape:", train_data.shape) clf = ImageClassifier(verbose=True) clf.fit(train_data, train_labels, time_limit=TIME) clf.final_fit(train_data, train_labels, test_data, test_labels, retrain=True) y = clf.evaluate(test_data, test_labels) print("Evaluate:", y) #Predict the category of the test image img = load_img(PREDICT_IMG_PATH) x = img_to_array(img) x = x.astype('float32') / 255 x = np.reshape(x, (1, RESIZE, RESIZE, 3)) print("x shape:", x.shape) y = clf.predict(x) print("predict:", y) clf.load_searcher().load_best_model().produce_keras_model().save(MODEL_DIR) #Save model architecture diagram model = load_model(MODEL_DIR) plot_model(model, to_file=MODEL_PNG)
def train_autokeras(self): time_limit = self.projectinfo['parameter_time'] #Load images train_data, train_labels = load_image_dataset(csv_file_path=self.project_train_labels_csv, images_path=self.project_resize_train_dir) test_data, test_labels = load_image_dataset(csv_file_path=self.project_test_labels_csv, images_path=self.project_resize_test_dir) train_data = train_data.astype('float32') / 255 test_data = test_data.astype('float32') / 255 self.log.info("Train data shape: %d" % train_data.shape[0]) clf = ImageClassifier(verbose=True, path=self.project_tmp_dir, resume=False) clf.fit(train_data, train_labels, time_limit=time_limit) clf.final_fit(train_data, train_labels, test_data, test_labels, retrain=True) evaluate_value = clf.evaluate(test_data, test_labels) self.log.info("Evaluate: %f" % evaluate_value) clf.export_autokeras_model(self.project_mod_path) #统计训练信息 dic = {} ishape = clf.cnn.searcher.input_shape dic['n_train'] = train_data.shape[0] #训练总共用了多少图 dic['n_classes'] = clf.cnn.searcher.n_classes dic['input_shape'] = str(ishape[0]) + 'x' + str(ishape[1]) + 'x' + str(ishape[2]) dic['history'] = clf.cnn.searcher.history dic['model_count'] = clf.cnn.searcher.model_count dic['best_model'] = clf.cnn.searcher.get_best_model_id() best_model = [item for item in dic['history'] if item['model_id'] == dic['best_model']] if len(best_model) > 0: dic['loss'] = best_model[0]['loss'] dic['metric_value'] = best_model[0]['metric_value'] dic['evaluate_value'] = evaluate_value return dic
# In[ ]: get_ipython().system(u'mkdir models') clf = ImageClassifier(path="models/", verbose=True) clf.fit(x_train, y_train, time_limit=hours_for_training * 60 * 60) # In[ ]: x_val, y_val = load_image_dataset(csv_file_path="split_data/val/label.csv", images_path="split_data/resized-val/") print(x_val.shape) print(y_val.shape) # In[ ]: clf.final_fit(x_train, y_train, x_val, y_val, retrain=True) y = clf.evaluate(x_val, y_val) print(y) # In[ ]: x_test, y_test = load_image_dataset(csv_file_path="split_data/test/label.csv", images_path="split_data/resized-test/") print(x_test.shape) print(y_test.shape) # In[ ]: y = clf.evaluate(x_test, y_test) print(y)
if __name__ == '__main__': # 需要把数据放到 ~/.keras/dataset 中,不然下载会报错 (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) # (60000, 28, 28) print('增加一个维度,以符合格式要求') x_train = x_train.reshape(x_train.shape + (1, )) print(x_train.shape) # (60000, 28, 28, 1) x_test = x_test.reshape(x_test.shape + (1, )) # 指定模型更新路径 clf = ImageClassifier(path="automodels/", verbose=True) # 限制为 4 个小时 # 搜索部分 gap = 6 clf.fit(x_train[::gap], y_train[::gap], time_limit=4 * 60 * 60) # 用表现最好的再训练一次 clf.final_fit(x_train[::gap], y_train[::gap], x_test, y_train, retrain=True) y = clf.evaluate(x_test, y_test) print(y) print("导出训练好的模型") clf.export_autokeras_model("automodels/auto_mnist_model") print("可视化模型") visualize("automodels/")
from keras.datasets import mnist from autokeras.image.image_supervised import ImageClassifier (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(x_train.shape + (1, )) x_test = x_test.reshape(x_test.shape + (1, )) clf = ImageClassifier(verbose=True) #clf.fit(x_train, y_train, time_limit=12 * 60 * 60) ## 300 seconds -> Search Time too short. No model was found during the search time clf.fit(x_train, y_train, time_limit=10 * 60 * 60) # 1 hour clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) print(y)
test_data, test_labels = load_image_dataset(csv_file_path=TEST_CSV_DIR, images_path=TEST_IMG_DIR) # 数据进行格式转换 train_data = train_data.astype('float32') / 255 test_data = test_data.astype('float32') / 255 print("train data shape:", train_data.shape) # 使用图片识别器 clf = ImageClassifier(verbose=True) # 给其训练数据和标签,训练的最长时间可以设定,假设为1分钟,autokers会不断找寻最优的网络模型 clf.fit(train_data, train_labels, time_limit=1 * 60) # 找到最优模型后,再最后进行一次训练和验证 clf.final_fit(train_data, train_labels, test_data, test_labels, retrain=True) # 给出评估结果 y = clf.evaluate(test_data, test_labels) print("evaluate:", y) # 给一个图片试试预测是否准确 img = load_img(PREDICT_IMG_PATH) x = img_to_array(img) x = x.astype('float32') / 255 x = np.reshape(x, (1, IMAGE_SIZE, IMAGE_SIZE, 3)) print("x shape:", x.shape) # 最后的结果是一个numpy数组,里面是预测值4,意味着是马,说明预测准确 y = clf.predict(x)
from keras.datasets import mnist from autokeras.image.image_supervised import ImageClassifier # gather data (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(x_train.shape + (1, )) x_test = x_test.reshape(x_test.shape + (1, )) # train the model model = ImageClassifier(verbose=True) model.fit(x_train, y_train, time_limit=15 * 60) model.final_fit(x_train, y_train, x_test, y_test, retrain=False) y = model.evaluate(x_test, y_test) print(y)
num_images = 47 x_train = [] for i in range (1,num_images+1): img = cv2.imread("images/" + str(i) + ".jpg") resized_image = cv2.resize(img, (100,100)) x_train.append(resized_image) print(len(x_train)) x_train = np.array(x_train) y_train = [5,5,5,4,7,8,6,6,8,10,9,7,7,8,8,9,6,9,4,4,9,3,8,5,1,3,2,6,7,4,7,6,6,2,3,8,3,7,8,4,6,9,5,10,1,0,10] x_test = [] img = cv2.imread("images/" + str(num_images) + ".jpg") resized_image = cv2.resize(img, (100,100)) x_test.append(resized_image) x_test = np.array(x_test) y_test = [num_images] x_train = x_train.reshape(x_train.shape + (1,)) x_test = x_test.reshape(x_test.shape + (1,)) clf = ImageClassifier(verbose=True) clf.fit(x_train, y_train, time_limit=2*60) ''' clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) clf.load_searcher().load_best_model().produce_keras_model().save('test_save.h5') '''