コード例 #1
0
    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
コード例 #2
0
ファイル: main_SDK.py プロジェクト: paulxiong/cervical
    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
コード例 #3
0
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/")
コード例 #4
0
    x_train.append(img)
    # x_train.reshape(256,256,3)
    y_train.append(0)
x_train = np.array(x_train)
y_train = np.array(y_train)

for file_name in os.listdir("test/normal"):
    img = cv2.imread("test/normal/" + file_name)
    x_test.append(img)
    # x_train.reshape(256,256,3)
    y_test.append(0)

for file_name in os.listdir("test/anomaly"):
    img = cv2.imread("test/anomaly/" + file_name)
    x_test.append(img)
    # x_train.reshape(256,256,3)
    y_test.append(0)
x_test = np.array(x_test)
y_test = np.array(y_test)

print(x_train.shape)
print(y_train.shape)

clf = ImageClassifier(verbose=True)
clf.fit(x_train, y_train, time_limit=12 * 60 * 60)

clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
clf.export_autokeras_model("./autokeras_model.bin")  # Auto-Kerasで読み込めるモデルを保存
clf.export_keras_model("./keras_model.bin")  # Kerasで読み込めるモデルを保存

acc = clf.evaluate(x_test, y_test)
コード例 #5
0
ファイル: script.py プロジェクト: anicksaha/visual-embedding
from autokeras.image.image_supervised import load_image_dataset
from autokeras.image.image_supervised import ImageClassifier


x_train, y_train = load_image_dataset(csv_file_path="../data-mnist/train_label.csv", images_path="../data-mnist/train")
print(len(x_train))
# print(x_train.shape)
# print(y_train.shape)

x_test, y_test = load_image_dataset(csv_file_path="../data-mnist/test_label.csv", images_path="../data-mnist/test")
# print(x_test.shape)
# print(y_test.shape)

clf = ImageClassifier(verbose=True)

clf.fit(x_train, y_train, time_limit= 14 * 60 * 60) # 14 hours

clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)

y = clf.evaluate(x_test, y_test)

print(y)

model_file_name = "mnist_1_hour"
clf.export_autokeras_model(model_file_name)


コード例 #6
0
    # x_test = x_test.reshape(x_test.shape + (1,))

    # clf = ImageClassifier(path='output/', verbose=True, searcher_args={
    #                       'trainer_args': {'max_iter_num': 1,
    #                                        'max_no_improvement_num': 1}})
    # clf.fit(x_train, y_train, time_limit=1 * 60 * 30)
    # clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    # y = clf.evaluate(x_test, y_test)
    # print(y)

    (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, searcher_args={'trainer_args':{'max_iter_num':7}})
    clf.fit(x_train, y_train, time_limit=12 * 60 * 60)
    clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    y = clf.evaluate(x_test, y_test)
    print(y)

    clf.export_autokeras_model('output/auto_mnist_model')

    # alternative
    best_model = clf.cnn.best_model.produce_model()
    pickle_to_file(best_model, 'output/auto_mnist_best_model')
    print(best_model)

# Step 2 : After the model training is complete, run examples/visualize.py, whilst passing the same path as parameter
# if __name__ == '__main__':
#    visualize('~/automodels/')
コード例 #7
0
ファイル: nas.py プロジェクト: anicksaha/visual-embedding
y_test = []

base_path = "../data-deep-fashion-women/img/"

#Load the data from local file into a dataframe
df = pd.read_csv('../data-deep-fashion-women/img/WOMEN/labels_test.csv')
print(len(df))

for index, row in df.iterrows():
    #print(row[0], row[1])
    ss = base_path + row[0]
    #print(ss)
    img = image.load_img(ss, target_size=(224, 224))
    img_data = image.img_to_array(img)
    image_data_np = np.array(img_data)
    x_test.append(image_data_np)
    y_test.append(row[1])

from autokeras.image.image_supervised import load_image_dataset
from autokeras.image.image_supervised import ImageClassifier

clf = ImageClassifier(verbose=True)
clf.fit(x_train, y_train, time_limit=10 * 60 * 60)  # 10 hours
clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
y = clf.evaluate(x_test, y_test)
print(y)

clf.export_autokeras_model('./_models/nas_1.h5')
clf.export_keras_model('./_models/nas_2.h5')
clf.load_searcher().load_best_model().produce_keras_model().save(
    './_models/nas_3.h5')