def run(): x_train, y_train, x_test, y_test = load_images() # After loading train and evaluate classifier. clf = ImageClassifier(verbose=True, augment=False) 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 * 100)
def prepare(self, path, x_train, y_train): global clf # TODO: find a better way to make our own Searcher resume = False if os.path.isdir(self.path): resume = True print(f"Resuming") # TODO: Fix resume after debug clf = self.clf = ImageClassifier(verbose=True, augment=False, path=path, resume=False, search_type=ObservableSearcher)
def train(auto_keras_config_id): print('auto_keras_config object: ' + str(auto_keras_config_id)) auto_keras_config = AutoKerasConfig.objects.get(id=auto_keras_config_id) auto_keras_config.status = 'in_progress' auto_keras_config.save() # Storing save location for models try: dump_file = os.path.join(AUTO_ML_MODELS_PATH, 'auto_keras' + str(datetime.datetime.now()) + '.h5') print('Files to load: ' + auto_keras_config.training_data_filename) x = numpy.load(os.path.join(AUTO_ML_DATA_PATH, auto_keras_config.training_data_filename)) y = numpy.load(os.path.join(AUTO_ML_DATA_PATH, auto_keras_config.training_labels_filename)) # x, y = load_ml_data(auto_keras_config.training_data_filename, auto_keras_config.training_labels_filename, False, auto_keras_config.make_one_hot_encoding_task_binary) # TODO this might not work on low ram machines work, but array has to be 3d if auto_keras_config.preprocessing_object.input_data_type == 'wav': array4d = [] i=0 for datapoint in x: print(i) x_3d = datapoint.reshape((172,128,10)).transpose() array4d.append(x_3d) i+=1 x = numpy.array(array4d) clf = ImageClassifier(verbose=auto_keras_config.verbose) start = time.time() clf.fit(x, y, time_limit=auto_keras_config.time_limit) end = time.time() #clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) #y = clf.evaluate(x_test, y_test) print("Fitting Success!!!") # storing the best performer clf.export_autokeras_model(dump_file) print('saved!') # auto_keras_config.training_time = round(end-start, 2) auto_keras_config.status = 'success' auto_keras_config.model_path = dump_file auto_keras_config.save() print('Status final ' + auto_keras_config.status) except Exception as e: end = time.time() if 'start' in locals(): print('failed after:' + str(end-start)) auto_keras_config.training_time = round(end-start, 2) auto_keras_config.status = 'fail' auto_keras_config.additional_remarks = e auto_keras_config.save()
import os from keras.datasets import mnist from autokeras import ImageClassifier from autokeras import pickle_from_file # Customer temp dir by your own TEMP_DIR = '/tmp/autokeras_U8KEOQ' model_file_name = os.path.join(TEMP_DIR, 'test_autokeras_model.pkl') if __name__ == '__main__': (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, augment=False, path=TEMP_DIR, resume=True) clf.fit(x_train, y_train, time_limit=30 * 60) clf.final_fit(x_train, y_train, x_test, y_test) clf.export_autokeras_model(model_file_name) model = pickle_from_file(model_file_name) results = model.evaluate(x_test, y_test) print(results)
import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt from autokeras import ImageClassifier clf = ImageClassifier(searcher_type='bayesian', path='/Users/haifeng/cifar10_backup', resume=True) searcher = clf.load_searcher() kernel_matrix = searcher.gpr.kernel_matrix history = searcher.history diff = np.zeros(kernel_matrix.shape) for i, item1 in enumerate(history): for j, item2 in enumerate(history): e1 = item1['accuracy'] e2 = item2['accuracy'] diff[i][j] = 1 - abs(e1 - e2) m1 = np.zeros(kernel_matrix.shape) m2 = np.zeros(kernel_matrix.shape) m3 = np.zeros(kernel_matrix.shape) for i in range(47): for j in range(47): m1[i][j] = np.where( np.array(sorted(kernel_matrix.flatten())) == kernel_matrix[i] [j])[0][0] m2[i][j] = np.where( np.array(sorted(diff.flatten())) == diff[i][j])[0][0] m1[i][j] = norm.ppf(m1[i][j] / (47.0 * 47.0)) m2[i][j] = norm.ppf(m2[i][j] / (47.0 * 47.0)) m3[i][j] = abs(m1[i][j] - m2[i][j])
# scale the raw pixel intensities to the range [0, 1] trainX = np.array(trainX, dtype="float16") / 255.0 testX = np.array(testX, dtype="float16") / 255.0 trainY = np.array(trainY) testY = np.array(testY) print("[INFO] data matrix: {:.2f}MB".format(trainX.nbytes / (1024 * 1000.0))) print("[INFO] data shape : {}".format(trainX.shape)) print("[INFO] label shape : {}".format(trainY.shape)) # trainX = trainX.reshape(trainX.shape + (1,)) # testX = testX.reshape(testX.shape + (1,)) print(trainX.shape, trainY.shape, testX.shape, testY.shape) clf = ImageClassifier(path='autokeras_output/', verbose=True, augment=False) clf.fit(trainX, trainY, time_limit=12 * 60 * 60) clf.final_fit(trainX, trainY, testX, testY, retrain=True) y = clf.evaluate(testX, testY) print(y * 100) joblib.dump(clf, 'wfc3_autokeras_model.joblib.save') ''' from keras.datasets import mnist from autokeras import ImageClassifier if __name__ == '__main__': (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,))
from keras.datasets import mnist from autokeras import ImageClassifier # loadning mnist from keras (X_train, y_train), (X_test, y_test) = 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,)) clf = ImageClassifier(verbose=True, augment=True) clf.fit(X_train, y_train, time_limit=(1*60*60))
from keras.datasets import mnist from autokeras import ImageClassifier import tensorflow if __name__ == '__main__': print(tensorflow.__version__) (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, augment=False) 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) print(y * 100)
from keras.datasets import mnist from autokeras import ImageClassifier from matplotlib import pyplot as plt if __name__ == '__main__': (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, )) # Strange!! # nest line actually calls: # NetworkModule class from: # anaconda3/envs/auto-keras/lib/python3.6/site-packages/autokeras/net_module.py # cos code: "from autokeras import ImageClassifier" # when you execute it, it will call autokeras from anaconda env's site-packages # however the grammar checker and the hyper-linker will track it from the local package!!! # they are different!!!!! clf = ImageClassifier(verbose=True, augment=False) clf.fit(x_train, y_train, time_limit=30 * 6000) clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) plt.imshow(x_train[0, :, :, 0], cmap=plt.cm.bone) print(y * 100)
import os from keras.datasets import mnist from autokeras import ImageClassifier from autokeras.utils import pickle_from_file # Customer temp dir by your own TEMP_DIR = '/tmp/autokeras_U8KEOQ' model_file_name = os.path.join(TEMP_DIR, 'test_autokeras_model.pkl') if __name__ == '__main__': (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, augment=False, path=TEMP_DIR, resume=True) clf.fit(x_train, y_train, time_limit=30 * 60) clf.final_fit(x_train, y_train, x_test, y_test) clf.export_autokeras_model(model_file_name) model = pickle_from_file(model_file_name) results = model.evaluate(x_test, y_test) print(results)
print(y_train) for i in range(5): n = i*20 img = x_train[n,:,:,:].reshape((28,28)) print(y_train[n]) plt.figure() plt.imshow(img,cmap='gray') plt.xticks([]) plt.yticks([]) plt.show() ''' if __name__ == '__main__': start = time.time() # 模型构建 model = ImageClassifier(verbose=True) # 搜索网络模型 model.fit(x_train, y_train, time_limit=1 * 60) # 验证最优模型 model.final_fit(x_train, y_train, x_train, y_train, retrain=True) # 给出评估结果 score = model.evaluate(x_train, y_train) # 识别结果 y_predict = model.predict(x_train) # y_pred = np.argmax(y_predict,axis=1) # 精确度 accuracy = accuracy_score(y_train, y_predict) # 打印出score与accuracy print('score:', score, ' accuracy:', accuracy) print(y_predict, y_train) model_dir = r'./trainer/new_auto_learn_Model.h5'
from keras.datasets import mnist from autokeras import ImageClassifier from autokeras.constant import Constant if __name__ == '__main__': (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, augment=False) clf.fit(x_train, y_train, time_limit=30 * 60) clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) y = clf.evaluate(x_test, y_test) print(y * 100) clf.export_autokeras_model('my_model-1.h5')
from autokeras import ImageClassifier from tensorflow.keras.datasets import fashion_mnist as fm (X_train, y_train), (X_test, y_test) = fm.load_data() X_train = X_train.astype('float32') / 255.0 X_test = X_test.astype('float32') / 255.0 EPOCHS = 10 classifier = ImageClassifier(seed=9, max_trials=10) classifier.fit(X_train, y_train, epochs=EPOCHS, verbose=2) print(classifier.evaluate(X_test, y_test))
import os # path = '/Users/arron/autokeras/' # os.chdir(path) from keras.datasets import mnist # from autokeras.image.image_supervised import ImageClassifier from autokeras 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,)) # 1. 将 verbose 指定为 True 意味着搜索过程将打印在屏幕上供我们查看 clf = ImageClassifier(verbose=True) # 2. 在 fit 方法中,time_limit 参数是指以秒为单位的搜索时间限制 clf.fit(x_train, y_train, time_limit=12 * 60 * 60) # 3. final_fit 是模型找到最佳模型架构后进行的最后一次训练。将 retrain 参数指定为 True 意味着将重新初 始化模型的权重 clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) # 4. 在评估模型在测试集上的效果后,print(y) 将显示模型准确率 y = clf.evaluate(x_test, y_test) print(y) # export the model clf.export_keras_model('my_model.h5') # # from keras.models import load_model # model = load_model('my_model.h5') # # from keras.utils import plot_model # plot_model(model, to_file='my_model.png')
from keras.datasets import mnist from autokeras import ImageClassifier if __name__ == '__main__': (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, augment=False) 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 * 100)
""" #Fixed the error, the excel file contained the DS_Store file #Loading the dataset x_train, y_train = load_image_dataset(csv_file_path=train_csv, images_path=train_dir) x_test, y_test = load_image_dataset(csv_file_path=test_csv, images_path=test_dir) x_train = x_train.reshape(x_train.shape + (1, )) x_test = x_test.reshape(x_test.shape + (1, )) clf = ImageClassifier(verbose=True) #Chose to set the training time to 3600s for testing purposes clf.fit(x_train, y_train, time_limit=(60 * 60 * 9.5)) clf.final_fit(x_train, y_train, x_test, y_test, retrain=True) #y gives us the accuracy of our structure y = clf.evaluate(x_test, y_test) print(y) #Verfying if the images are corrupt for filename in listdir(dir_path): if filename.endswith('.jpg'): try: img = image.open(dir_path + "/" + filename) # open the image file img.verify() # verify that it is, in fact an image except (IOError, SyntaxError) as e: print('Bad file:', filename)
test_label_list.append(plant_folder) print("[INFO] Image loading completed") except Exception as e: print(f"Error : {e}") y_test_ = label_gener.transform(test_label_list) y_test = [[i] for i in y_test_] y_test = np.array(y_test) x_test = np.array(test_image_list) / 225.0 #scailing 0~1 #______________________MODEL DEF____________________________ clf = ImageClassifier(verbose=True, path='auto-keras/', searcher_args={'trainer_args': { 'max_iter_num': 30 }}) clf.fit(x_train, y_train, time_limit=2 * 60 * 60) # hour print(clf.get_best_model_id()) searcher = clf.load_searcher() print(searcher.history) #view all model result # FINAL train with best model clf.final_fit(x_train, y_train, x_test, y_test, retrain=False, trainer_args={'max_iter_num': 3})
def init(path, x_train, y_train): global clf # TODO: find a better way to make our own Searcher clf = ImageClassifier(verbose=True, augment=False, path=path, resume=True) searcher = ObservableSearcher(clf, x_train.shape[1:], y_train)
import numpy as np from autokeras import ImageClassifier from tests.common import clean_dir if __name__ == '__main__': x_train = np.random.rand(2, 28, 28, 1) y_train = np.random.rand(2) x_test = np.random.rand(1, 28, 28, 1) y_test = np.random.rand(1) trainer_args = {'max_iter_num': 0, 'batch_size': 128, 'augment': False} clf = ImageClassifier(path='tests/resources/temp', verbose=True, searcher_args={'trainer_args': trainer_args}) clf.fit(x_train, y_train, time_limit=30) clf.load_searcher().export_json('./test.json') clean_dir('tests/resources/temp')