def training_model(model_name='mobilenet'): train_img, valid_img, train_y, valid_y = get_data() callbacks = get_callbacks('mobilenet_10fulld01_b16', patience=2) if model_name == 'mobilenet': print('MobileNet') model = MobileNet(alpha=1.) model.summary() elif model_name == 'mobilenet_dih': print('MobileNetDih') model = MobileNetDih4(alpha=1.) model.summary() elif model_name == 'mobilenet_dih_r': print('MobileNetDihR') model = MobileNetDR(alpha=1.) model.summary() opt = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999) #opt = Adadelta(lr=1e-1, rho=0.95, decay=0.1) #opt = SGD(lr=1e-7, momentum=0.9, decay=0., nesterov=True) model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy']) #model.load_weights('mobilenet_05shortd01_catcros_resize_b16.hdf5') gen = ImageDataGenerator(rotation_range=359, zoom_range=[0.5, 2], width_shift_range=0.1, height_shift_range=0.1, vertical_flip=True, horizontal_flip=True) model.fit_generator( gen.flow(np.array(train_img), np.array(train_y), batch_size=BATCH_SIZE), steps_per_epoch=16 * len(train_y) // BATCH_SIZE, epochs=40, validation_data=[np.array(valid_img), np.array(valid_y)], verbose=1, callbacks=callbacks) # """ #opt = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999) #opt = Adadelta(lr=1e-1, rho=0.95, decay=0.1) opt = SGD(lr=0.05, momentum=0.9, decay=0., nesterov=True) model.load_weights('mobilenet_10shortd01_b16_sgd') model.fit_generator( gen.flow(np.array(train_img), np.array(train_y), batch_size=BATCH_SIZE), steps_per_epoch=16 * len(train_y) // BATCH_SIZE, epochs=10, validation_data=[np.array(valid_img), np.array(valid_y)], verbose=1, callbacks=callbacks)
# 神兽保佑 # BUG是不可能有BUG的! import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' from mobilenet import MobileNet import numpy as np np.random.seed(10) from keras.datasets import cifar10 from keras.utils import np_utils (x_img_train, y_label_train),(x_img_test, y_label_test) = cifar10.load_data() x_img_train = x_img_train.astype('float')/255.0 x_img_test = x_img_test.astype('float')/255.0 y_label_train = np_utils.to_categorical(y_label_train) y_label_test = np_utils.to_categorical(y_label_test) model = MobileNet() try: model.load_weights("mobileV1-lite.h5") print("模型加载成功!继续训练") except: print("模型加载失败!从头开始训练") model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) train_history = model.fit(x_img_train, y_label_train, validation_split=0.2, epochs=10, batch_size=128, verbose=2) model.save_weights("mobileV1-lite.h5") print("保存模型成功!")
import keras from keras.models import load_model from keras.preprocessing import image from keras.preprocessing.image import load_img from mobilenet import MobileNet from keras.applications.imagenet_utils import decode_predictions img_file = 'demo.png' img = load_img(img_file, target_size=(32, 32)) image = image.img_to_array(img) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) model = MobileNet() model.load_weights('mobileV1-lite.h5') # model.summary() result = model.predict(image) print(result)
from mobilenet import MobileNet from utils import load_mnist os.chdir("../") PATH = './handwritten_digit_recognition/models/' #model_name = "WideResNet28_10" #model=WideResNet28_10() model_name = "MobileNet" model = MobileNet() model.compile() model2 = MobileNet() model2.compile() print('Loading pretrained weights for ', model_name, '...', sep='') model.load_weights(PATH + model_name + "_ajustado_1" + '.h5') model2.load_weights(PATH + model_name + '.h5') def mesaImagen(mesa, boleta=1): fname = 'actas/{0:06d}'.format(mesa * 10 + boleta) + '.jpg' data_name = "mesas_rv/" + '{}'.format(mesa) + '.json' path = "./" out_path = './results/' exists = os.path.isfile(path + fname) if not exists: print("No se encontró el scan de la mesa ", mesa, ", boleta ", boleta) return None