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 load_images(): x_train, y_train = load_image_dataset(csv_file_path="train/label.csv", images_path="train") print(x_train.shape) print(y_train.shape) x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test") print(x_test.shape) print(y_test.shape) return x_train, y_train, x_test, y_test
def load_images(): x_train, y_train = load_image_dataset(csv_file_path="train/label.csv", images_path="train") print(x_train.shape) print(y_train.shape) x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test") print(x_test.shape) print(y_test.shape) return x_train, y_train, x_test, y_test
def train_model(): clf = ak.ImageClassifier(verbose=True, augment=False) train_data, train_labels = load_image_dataset(csv_file_path=train_data_dir+"/label.csv", images_path=train_data_dir) validation_data, validation_labels = load_image_dataset(csv_file_path=validation_data_dir+"/label.csv", images_path=validation_data_dir) clf.fit(train_data, train_labels) clf.final_fit(train_data, train_labels, validation_data, validation_labels, retrain=True) y = clf.evaluate(validation_data, validation_labels) print("auto CNN classifier accuracy: %f" % y) clf.load_searcher().load_best_model().produce_keras_model().save('shallowCNN_model.h5')
def load_images(): x_train, y_train = load_image_dataset(csv_file_path=data_dir + "/labels_real.csv", images_path=data_dir + "/train") print(x_train.shape) print(y_train.shape) x_test = load_image_dataset(csv_file_path=data_dir + "/sample_submission_real.csv", images_path=data_dir + "/test") print(x_test[0].shape) return x_train, y_train, x_test[0]
def load_invasive_species(): data_dir = "datasets/invasive-species" x_train, y_train = load_image_dataset(csv_file_path=data_dir + "/train_labels_real.csv", images_path=data_dir + "/train") print(x_train.shape) print(y_train.shape) x_test, y_test = load_image_dataset(csv_file_path=data_dir + "/sample_submission_real.csv", images_path=data_dir + "/test") print(x_test.shape) return x_train, y_train, x_test
def load_plant_seedlings(): data_dir = "datasets/plant-seedlings" x_train, y_train = load_image_dataset( csv_file_path=data_dir + "/train.csv", images_path=data_dir + "/train_files") print(x_train.shape) print(y_train.shape) x_test = load_image_dataset(csv_file_path=data_dir + "/sample_submission.csv", images_path=data_dir + "/test") print(x_test[0].shape) return x_train, y_train, x_test[0]
def load_aerial_cactus(): data_dir = "datasets/aerial-cactus" x_train, y_train = load_image_dataset(csv_file_path=data_dir + "/train.csv", images_path=data_dir + "/train") print(x_train.shape) print(y_train.shape) x_test = load_image_dataset(csv_file_path=data_dir + "/sample_submission.csv", images_path=data_dir + "/test") print(x_test[0].shape) return x_train, y_train, x_test[0]
def predict_autokeras(self): #Load images test_data, test_labels = load_image_dataset(csv_file_path=self.PREDICT_CSV_DIR, images_path=self.RESIZE_PREDICT_IMG_DIR) test_data = test_data.astype('float32') / 255 print("Test data shape:", test_data.shape) autokeras_model = pickle_from_file(self.MODEL_DIR) autokeras_score = autokeras_model.evaluate(test_data, test_labels) print(autokeras_score)
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
from autokeras.image.image_supervised import load_image_dataset # clf = ak.ImageClassifier(verbose=True, augment=False) clf = ak.ImageClassifier( path= "/home/mayank_sati/pycharm_projects/tensorflow/traffic_light_detection_classification-master/traffic_light_classification/autokeras/visualise", verbose=True, augment=False) # clf = ImageClassifier(path="~/automodels/",verbose=True, augment=False) train_image_path = "/home/mayank_sati/Desktop/sorting_light/all_train_images" test_image_path = "/home/mayank_sati/Desktop/sorting_light/all_train_images" train_csv = "/home/mayank_sati/Desktop/sorting_light/train_color_autokeras.csv" test_csv = "/home/mayank_sati/Desktop/sorting_light/test_color_autokeras.csv" x_train, y_train = load_image_dataset(csv_file_path=train_csv, images_path=train_image_path) print(x_train.shape) print(y_train.shape) x_test, y_test = load_image_dataset(csv_file_path=test_csv, images_path=train_image_path) print(x_test.shape) print(y_test.shape) # clf = ImageClassifier(verbose=True) clf.fit(x_train, y_train, time_limit=10 * 60) clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) print(y)
from autokeras.image.image_supervised import ImageClassifier, load_image_dataset # we created ./all path where we copied all the images train_path = '../data/all' train_labels = '../data/labels_train.csv' x_train, y_train = load_image_dataset(csv_file_path=train_labels, images_path=train_path) #x_val, y_val = load_image_dataset(csv_file_path=validation_labels,images_path=validation_path) clf = ImageClassifier(verbose=True) # 4 hours search clf.fit(x_train, y_train, time_limit=4 * 60 * 60) best_model = clf.export_keras_model() keras_model = best_model.produce_keras_model('asdf') keras_model.summary() # save it keras_model.save('best.hdf5') #clf.final_fit(x_train,y_train,x_val,y_val,retrain = True, trainer_args={'max_iter_num':10}) #print(clf.evaluate(x_val,y_val))
# In[ ]: size_of_image = 1024 # In[ ]: hours_for_training = float(input("How many hours to train (in decimals)?")) # In[ ]: from autokeras.image.image_supervised import load_image_dataset # In[ ]: x_train, y_train = load_image_dataset( csv_file_path="split_data/train/label.csv", images_path="split_data/resized-train/") print(x_train.shape) print(y_train.shape) # In[ ]: from autokeras.image.image_supervised import ImageClassifier # 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[ ]:
from keras.utils import plot_model from keras.models import load_model from keras.models import Sequential from autokeras.utils import pickle_from_file import sys import numpy as np sys.path.append("/home/deep/PycharmProjects/autokeras") from autokeras.image.image_supervised import load_image_dataset x_test_keyhole, y_test_keyhole = load_image_dataset( csv_file_path="data_by_liuyang/test/test_keyhole.csv", images_path="data_by_liuyang/test/the_keyhole") print("the key hole:", x_test_keyhole.shape) print(y_test_keyhole.shape) x_test_no_keyhole, y_test_no_keyhole = load_image_dataset( csv_file_path="data_by_liuyang/test/test_no_keyhole.csv", images_path="data_by_liuyang/test/no_keyhole") print(x_test_no_keyhole.shape) print(y_test_no_keyhole.shape) x_test, y_test = np.vstack((x_test_keyhole, x_test_no_keyhole)), np.hstack( (y_test_keyhole, y_test_no_keyhole)) model_file_name = 'data_by_liuyang/model/autokeras.h5' #加载模型 #model = load_model('data_by_liuyang/model/autokeras.h5') model = pickle_from_file(model_file_name) #model = Sequential(model) #plot_model(model, to_file='my_model.png') results = model.evaluate(x_test, y_test) #用测试数据测试 print(results) #打印结果
from autokeras.image.image_supervised import load_image_dataset RESIZE_TRAIN_IMG_DIR = './data/resize/train' RESIZE_TEST_IMG_DIR = './data/resize/test' # Path to generate csv file TRAIN_CSV_DIR = './train_labels.csv' TEST_CSV_DIR = './test_labels.csv' 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 print('111' * 100) from keras.datasets import fashion_mnist (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data() # each image has to 3D: 2 coordinates, 1 value (gray scale) X_train = X_train.reshape(X_train.shape + (1, )) X_test = X_test.reshape(X_test.shape + (1, ))
# initialize the list of trianing times that we'll allow # Auto-Keras to train for TRAINING_TIMES = [ # 60 * 60, # 1 hour # 60 * 60 * 2, # 2 hours # 60 * 60 * 4, # 4 hours # 60 * 60 * 8, # 8 hours 60 * 60 * 12, # 12 hours # 60 * 60 * 24, # 24 hours ] # load the training and testing data, then scale it into the # range [0, 1] print("[INFO] loading 101_ObjectCategories data...") trainX, trainY = load_image_dataset(csv_file_path='./101_ObjectCategories/train/label.csv', images_path='./101_ObjectCategories/train') # testX, testY = load_image_dataset(csv_file_path='./101_ObjectCategories/test/label.csv', # images_path='./101_ObjectCategories/test') # trainX = trainX.astype("float") / 255.0 # testX = testX.astype("float") / 255.0 # initialize the label names for the CIFAR-10 dataset # labelNames = ['scorpion', 'hedgehog', 'anchor', 'joshua_tree', 'lobster', 'pyramid', 'ant', # 'dollar_bill', 'gerenuk', 'bass', 'trilobite', 'chandelier', 'dolphin', 'panda', # 'inline_skate', 'gramophone', 'camera', 'pizza', 'kangaroo', 'crocodile_head', # 'flamingo', 'hawksbill', 'ceiling_fan', 'lamp', 'lotus', 'ketch', 'cannon', 'stapler', # 'crocodile', 'wrench', 'scissors', 'umbrella', 'BACKGROUND_Google', 'minaret', 'crayfish', # 'ewer', 'cougar_body', 'okapi', 'Motorbikes', 'accordion', 'sunflower', 'headphone', 'bonsai', # 'mayfly', 'elephant', 'airplanes', 'cup', 'garfield', 'platypus', 'brontosaurus', 'butterfly', # 'euphonium', 'grand_piano', 'stegosaurus', 'nautilus', 'helicopter', 'rhino', 'wheelchair', # 'pagoda', 'laptop', 'tick', 'schooner', 'yin_yang', 'beaver', 'snoopy', 'watch', 'emu', 'dragonfly',
import sys sys.path.append("/home/deep/PycharmProjects/autokeras") from autokeras.image.image_supervised import load_image_dataset import numpy as np x_train_keyhole, y_train_keyhole = load_image_dataset( csv_file_path="deal-data/train/train_keyhole.csv", images_path="deal-data/train/the_keyhole") print(x_train_keyhole.shape) print(y_train_keyhole.shape) x_train_no_keyhole, y_train_no_keyhole = load_image_dataset( csv_file_path="deal-data/train/train_no_keyhole.csv", images_path="deal-data/train/no_keyhole") print(x_train_no_keyhole.shape) print(y_train_no_keyhole.shape) #将训练数据加起来 x_train, y_train = np.vstack([x_train_keyhole, x_train_no_keyhole]), np.hstack( [y_train_keyhole, y_train_no_keyhole]) print("x_train:", x_train.shape) print("y_train", y_train.shape) x_test_keyhole, y_test_keyhole = load_image_dataset( csv_file_path="deal-data/test/test_keyhole.csv", images_path="deal-data/test/the_keyhole") print("the key hole:", x_test_keyhole.shape) print(y_test_keyhole.shape) x_test_no_keyhole, y_test_no_keyhole = load_image_dataset( csv_file_path="deal-data/test/test_no_keyhole.csv", images_path="deal-data/test/no_keyhole") print(x_test_no_keyhole.shape)
from autokeras.utils import pickle_from_file from autokeras.image.image_supervised import load_image_dataset model_file_name = "mnist_1_hour" model = pickle_from_file(model_file_name) x_test, y_test = load_image_dataset( csv_file_path="./../data-mnist/test_label.csv", images_path="./../data-mnist/test") results = model.evaluate(x_test, y_test) print(results)
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)