from tensorflow.keras.optimizers import SGD from tensorflow.keras import layers from keras.layers.core import Activation from keras.models import Model from keras.layers.normalization import BatchNormalization from keras.layers.core import Dense from keras.layers import Input from keras.models import load_model from keras.callbacks import History history = History() import os from contextlib import contextmanager import time import gc CUR_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = os.path.join(CUR_DIR, "data") @contextmanager def timer(title): t0 = time.time() yield
def fit(self, env, nb_steps, action_repetition=1, callbacks=None, verbose=1, visualize=False, nb_max_start_steps=0, start_step_policy=None, log_interval=10000, nb_max_episode_steps=None,stepper=False): if not self.compiled: raise RuntimeError('Your tried to fit your agent but it hasn\'t been compiled yet. Please call `compile()` before `fit()`.') if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format(action_repetition)) self.training = True self.stepper = stepper callbacks = [] if not callbacks else callbacks[:] if verbose == 1: callbacks += [TrainIntervalLogger(interval=log_interval)] elif verbose > 1: callbacks += [TrainEpisodeLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_steps': nb_steps, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_train_begin() callbacks.on_train_begin() episode = 0 self.step = 0 observation = None episode_reward = None episode_step = None did_abort = False try: while self.step < nb_steps: if observation is None: # start of a new episode callbacks.on_episode_begin(episode) episode_step = 0 episode_reward = 0. # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint(nb_max_start_steps) for _ in range(nb_random_start_steps): if self.manual: action = int(raw_input("action?\n")) elif start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.shield is not None: if self.maze: inp = get_input_maze(observation) else: inp = get_input(observation) action_bin = to_bin(action) action = self.shield(inp[0],inp[1],inp[2],action_bin[0],action_bin[1],action_bin[2]) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) if self.stepper: action = int(raw_input("action?\n")) observation, reward, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, reward, done, info = self.processor.process_step(observation, reward, done, info) callbacks.on_action_end(action) if done: warnings.warn('Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.'.format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) break # At this point, we expect to be fully initialized. assert episode_reward is not None assert episode_step is not None assert observation is not None # Run a single step. callbacks.on_step_begin(episode_step) # This is were all of the work happens. We first perceive and compute the action # (forward step) and then use the reward to improve (backward step). #print observation if self.manual: oldaction = self.forward(observation, manual=True) else: oldaction = self.forward(observation,manual=False) # print oldaction if self.shield is not None: if self.maze: inp = get_input_maze(observation) else: inp = get_input(observation) action_bin = to_bin(oldaction) #sleep(0.01) action = to_int(self.shield.move(inp[0],inp[1],inp[2],inp[3],action_bin[0],action_bin[1],action_bin[2])) else: action = oldaction #print action, oldaction if self.processor is not None: action = self.processor.process_action(action) reward = 0. accumulated_info = {} done = False for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step(observation, r, done, info) for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value callbacks.on_action_end(action) reward += r if done: break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: # Force a terminal state. done = True metrics = self.backward(reward, terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward, 'metrics': metrics, 'episode': episode, 'info': accumulated_info, } oldstep_logs = { 'action': oldaction, 'observation': observation, 'reward': -1, 'metrics': metrics, 'episode': episode, 'info': accumulated_info, } # if correction: # callbacks.on_step_end(episode_step, oldstep_logs) # episode_step += 1 # self.step += 1 callbacks.on_step_end(episode_step, step_logs) episode_step += 1 self.step += 1 if done: # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # This episode is finished, report and reset. episode_logs = { 'episode_reward': episode_reward, 'nb_episode_steps': episode_step, 'nb_steps': self.step, } callbacks.on_episode_end(episode, episode_logs) episode += 1 observation = None episode_step = None episode_reward = None except KeyboardInterrupt: # We catch keyboard interrupts here so that training can be be safely aborted. # This is so common that we've built this right into this function, which ensures that # the `on_train_end` method is properly called. did_abort = True callbacks.on_train_end(logs={'did_abort': did_abort}) self._on_train_end() return history
def train(config): ''' Trains the model with fit generator and plots the results. ''' if config.MODEL == 'fcn8': from pcs_detection.models.fcn8_model import fcn8 elif config.MODEL == 'fcn_reduced': from pcs_detection.models.fcn8_reduced import fcn8 else: print('invalid model') # saving path will also add a time stamp to avoid duplicates and save per epoch dir_path = os.path.dirname(os.path.realpath(__file__)).rsplit( '/', 2)[0] + '/scripts' utid = datetime.datetime.now().strftime('%y_%m_%d_%H%M%S') model_info = config.MODEL + '_' + config.CHANNEL config.WEIGHT_SAVE_PATH = '{0}/{1}/{2}_{3}_{4}/{5}'.format( dir_path, config.WEIGHT_DIR, config.WEIGHT_ID, model_info, utid, '{epoch:02d}.h5') # reset umask so new permissions can be written original_umask = os.umask(0) #make the model save dir if it does not already exist if not os.path.exists(config.WEIGHT_SAVE_PATH): print("Making dir: {}".format(config.WEIGHT_SAVE_PATH)) os.makedirs(os.path.split(config.WEIGHT_SAVE_PATH)[0], mode=0o777) os.umask(original_umask) else: os.chmod(os.path.split(config.WEIGHT_SAVE_PATH)[0], 0o777) os.umask(original_umask) # never train on the full image config.USE_FULL_IMAGE = False # create the model weldDetector = fcn8(config) weldDetector.build_model() weldDetector = weldDetector.model # create the generator for the data DataLoader = dataLoader(config) DataLoader_val = dataLoader(config) DataLoader_val.mode = "VALIDATION" DataLoader_val.loadDataPaths() # callback to save the weight files checkpointer = ModelCheckpoint( filepath=config.WEIGHT_SAVE_PATH, verbose=1, save_best_only=True, mode='max', monitor='val_sparse_accuracy_ignoring_last_label_ohot', save_weights_only=False) # callback to reduce the learning rate reduce_lr = ReduceLROnPlateau( monitor='loss', factor=config.LEARNING_RATE['reduce_factor'], patience=config.LEARNING_RATE['reduce_patience'], min_lr=config.LEARNING_RATE['end'], verbose=1) # callback to stop early early_stop = EarlyStopping(monitor='loss', min_delta=0, patience=6, verbose=1, mode='auto') # training takes place here history = weldDetector.fit_generator( DataLoader.generate(), epochs=config.N_EPOCHS, steps_per_epoch=(DataLoader.num_paths // config.BATCH_SIZE) * config.AUGMENTATIONS['number_aug'], verbose=1, callbacks=[History(), checkpointer, reduce_lr, early_stop], use_multiprocessing=False, validation_data=DataLoader_val.generate(), validation_steps=DataLoader_val.num_paths // config.BATCH_SIZE) print("Finished training.") # generate a config for validation/inference using the best weights from training weights_dir = config.WEIGHT_SAVE_PATH.rsplit('/', 1)[0] best_weights_file = '01.h5' for file in sorted(os.listdir(weights_dir)): if file.endswith('.h5'): best_weights_file = file config.VAL_WEIGHT_PATH = os.path.join(weights_dir, best_weights_file) config.MODE = 'VALIDATE' dump_validation_config(config) dump_inference_config(config) # Display and save training curves fig = plt.figure() axis1 = fig.add_subplot(311) axis1.plot(history.history['sparse_accuracy_ignoring_last_label_ohot'], 'ro-') axis1.plot(history.history['val_sparse_accuracy_ignoring_last_label_ohot'], 'bo-') axis1.set_title('Training Metrics') axis1.set_ylabel('Accuracy') axis1.set_xlabel('Epoch') axis1.legend(['Train', 'Test'], loc='upper left') axis2 = fig.add_subplot(312) axis2.plot(history.history['loss'], 'mo-') axis2.plot(history.history['val_loss'], 'co-') #axis2.set_title('Model loss') axis2.set_ylabel('Loss') axis2.set_xlabel('Epoch') axis2.legend(['Train', 'Test', 'Train'], loc='upper left') axis3 = fig.add_subplot(313) axis3.plot(history.history['IoU'], 'mo-') axis3.plot(history.history['val_IoU'], 'co-') #axis3.set_title('Model IoU' + config.WEIGHT_SAVE_PATH) axis3.set_ylabel('IoU') axis3.set_xlabel('Epoch') axis3.legend(['Train', 'Test', 'Train'], loc='upper left') # saving them off in same folder as weights and config metrics_path = os.path.join( os.path.split(config.WEIGHT_SAVE_PATH)[0], 'metrics.png') print('saving figure to', metrics_path) fig.savefig(metrics_path) return
# n_transforms is the number of transformations to create for each image n_transforms = 10 X, y = load_train_data(m) # Ensure that we always use the same training and cross-validation sets # by always using 1 as the seed for the PRNG. np.random.seed(1) Xtr, Xval, ytr, yval = train_test_split(X, y, train_size=0.6, test_size=0.4) Xtr, ytr = aug.augment_dataset(Xtr, ytr, n_transforms, fixed_seeds=True) model = model_build_conv() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[categorical_accuracy]) my_hist = History() time_hist = TimeHistory() # Because we are feeding in an augmented dataset using model.fit, # the training categorical accuracy returned by the model will be # based on the augmented training set. However, we are far more # interested in the true training accuracy, based on predictions # from the unaugmented training set. The for loop below is an ugly hack. # I will replace it with a callback at some point. train_accs = [] for E in range(epochs): model.fit(Xtr, ytr, verbose=2, callbacks=[my_hist, time_hist], validation_data=(Xval, yval), epochs=E + 1,
def train_fine(self): # load coarse model print('### LOADING SAVED MODEL AND WEIGHTS ###') model_json = open(MODEL_FILE, 'r').read() model = model_from_json(model_json) model.load_weights(WEIGHTS_FILE) # freeze training on coarse layers for layer in model.layers: layer.trainable = False # modify with additional fine layers print('### UPDATING MODEL ###') # Input: inputs = model.inputs[0] # Fine 1: # 9x9 conv, 2 stride, ReLU activation, 2x2 pool fine_1 = Convolution2D(63, (9, 9), padding='same', kernel_initializer='uniform', strides=(2, 2), input_shape=(1, int(self.config['IMG_ROWS'] / 2), int(self.config['IMG_COLS'] / 2)), name='fine_1_conv')(inputs) #XXX fine_1 = Activation('relu', name='fine_1_relu')(fine_1) fine_1 = MaxPooling2D(pool_size=(2, 2), name='fine_1_pool')(fine_1) # Fine 2: # Concatenation with Coarse 7 coarse_out = model.outputs[0] coarse_out = Reshape((int( self.config['IMG_ROWS'] / 8), int(self.config['IMG_COLS'] / 8), 1), name='coarse_out_reshape')(coarse_out) #XXX fine_2 = merge([fine_1, coarse_out], mode='concat', concat_axis=3, name='fine_2_merge') # Fine 3: # 5x5 conv, 1 stride, ReLU activation, no pool fine_3 = Convolution2D(64, (5, 5), padding='same', kernel_initializer='uniform', strides=(1, 1), name='fine_3_conv')(fine_2) fine_3 = Activation('relu', name='fine_3_relu')(fine_3) # Fine 4: # 5x5 conv, 1 stride, linear activation, no pool fine_4 = Convolution2D(1, (5, 5), padding='same', kernel_initializer='uniform', strides=(1, 1), name='fine_4_conv')(fine_3) fine_4 = Activation('linear', name='fine_4_linear')(fine_4) fine_4 = Reshape((int( self.config['IMG_ROWS'] / 8), int(self.config['IMG_COLS'] / 8)), name='fine_4_reshape')(fine_4) #XXX # compile the model print('### COMPILING MODEL ###') model = Model(input=inputs, output=fine_4) model.compile(loss=self.scale_invariant_error, optimizer=SGD(lr=self.config['LEARNING_RATE'], momentum=self.config['MOMENTUM']), metrics=['accuracy']) model.summary() # save the model architecture to file print('### SAVING MODEL ARCHITECTURE ###') modelDir = dateTimeStr os.mkdir(os.path.join(self.config['OUTDIR'], modelDir)) modelFile = os.path.join( self.config['OUTDIR'], modelDir, 'depth_fine_model_{}.json'.format(dateTimeStr)) print(model.to_json(), file=open(modelFile, 'w')) # load and preprocess the data print('### LOADING DATA ###') X_train, Y_train = loadData( os.path.join(self.config['DATA_DIR'], 'train/')) X_test, Y_test = loadData( os.path.join(self.config['DATA_DIR'], 'test/')) print('X_train shape:', X_train.shape) print('Y_train shape:', Y_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # train the model #TODO use validation_split instead of using separate (test) data print('### TRAINING ###') history_cb = History() checkpointFile = os.path.join( self.config['OUTDIR'], modelDir, 'fine-weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5') checkpoint_cb = ModelCheckpoint(filepath=checkpointFile, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True, mode='auto') model.fit(X_train, Y_train, epochs=self.config['EPOCHS'], batch_size=self.config['BATCH_SIZE'], verbose=1, validation_split=0.2, callbacks=[history_cb, checkpoint_cb]) histFile = os.path.join(self.config['OUTDIR'], modelDir, 'depth_fine_hist_{}.json'.format(dateTimeStr)) # save the model weights to file print('### SAVING TRAINED MODEL ###') print(history_cb.history, file=open(histFile, 'w')) weightsFile = os.path.join( self.config['OUTDIR'], modelDir, 'depth_fine_weights_{}.h5'.format(dateTimeStr)) model.save_weights(weightsFile) # evaluate the trained model print('sleeping for 5 seconds...') time.sleep(5) print('### LOADING THE MODEL WEIGHTS ###') model_json = open(modelFile, 'r').read() model2 = model_from_json(model_json) model2.load_weights(weightsFile) model2.compile(loss=self.scale_invariant_error, optimizer=SGD(lr=self.config['LEARNING_RATE'], momentum=self.config['MOMENTUM']), metrics=['accuracy']) # evaluate the model print('### EVALUATING ###') score = model2.evaluate(X_test, Y_test, verbose=1) print('Test score:', score[0]) print('Test accuracy:', score[1])
def run_experiment(): print("* experiment configurations") print("===========================") print("Epoch count: {}".format(HYPER_PARAMS.num_epochs)) print("Image channel: {}".format(HYPER_PARAMS.num_channel)) with file_io.FileIO(HYPER_PARAMS.xtrain_files,'rb') as bi1: file_io.write_string_to_file('xtrain_files.hdf5',bi1.read()) with h5py.File('xtrain_files.hdf5', 'r') as f1: xtrain = np.asarray(f1['x_train'].value) # os.remove('xtrain_files.hdf5') with file_io.FileIO(HYPER_PARAMS.ytrain_files,'rb') as bi2: file_io.write_string_to_file('ytrain_files.hdf5',bi2.read()) with h5py.File('ytrain_files.hdf5', 'r') as f2: ytrain = np.asarray(f2['y_train'].value) # os.remove('ytrain_files.hdf5') with file_io.FileIO(HYPER_PARAMS.xval_files,'rb') as bi3: file_io.write_string_to_file('xval_files.hdf5',bi3.read()) with h5py.File('xval_files.hdf5', 'r') as f3: xval = np.asarray(f3['x_val'].value) # os.remove('xval_files.hdf5') with file_io.FileIO(HYPER_PARAMS.yval_files,'rb') as bi4: file_io.write_string_to_file('yval_files.hdf5',bi4.read()) with h5py.File('yval_files.hdf5', 'r') as f4: yval = np.asarray(f4['y_val'].value) # os.remove('yval_files.hdf5') #ytrain = np.asarray(f['ytrain'].value) #xval = np.asarray(f['xval'].value) #yval = np.asarray(f['yval'].value) # a = file_io.FileIO(HYPER_PARAMS.xtrain_files,'r') # b = file_io.FileIO(HYPER_PARAMS.ytrain_files,'r') # c = file_io.FileIO(HYPER_PARAMS.xval_files,'r') # d = file_io.FileIO(HYPER_PARAMS.yval_files,'r') # xtrain = np.load(a) # ytrain = np.load(b) # xval = np.load(c) # yval = np.load(d) # a.close() # b.close() # c.close() # d.close() print("X train shape: {}".format(xtrain.shape)) print("Y train shape: {}".format(ytrain.shape)) print("===========================") FMT_VALMODEL_PATH ="{}_val_weights.h5" FMT_VALMODEL_LAST_PATH = "{}_val_weights_last.h5" FMT_VALMODEL_HIST = "{}_val_hist.csv" PREFIX = HYPER_PARAMS.prefix INPUT_CHANNEL = HYPER_PARAMS.num_channel unet = model.get_unet(INPUT_CHANNEL) # train and evaluate model_checkpoint = ModelCheckpoint( FMT_VALMODEL_PATH.format(PREFIX + "_{epoch:02d}"), monitor='val_jaccard_coef_int', save_best_only=False, save_weights_only=True) model_earlystop = EarlyStopping( monitor='val_jaccard_coef_int', patience=20, verbose=0, mode='max') model_history = History() model_board = TensorBoard( log_dir=os.path.join(HYPER_PARAMS.job_dir, 'logs'), histogram_freq=0, write_graph=True, embeddings_freq=0) save_checkpoint_gcs = LambdaCallback( on_epoch_end=lambda epoch, logs: copy_file_to_gcs(HYPER_PARAMS.job_dir, FMT_VALMODEL_PATH.format(PREFIX + '_' + str(format(epoch + 1, '02d'))))) unet.fit( xtrain, ytrain, nb_epoch=HYPER_PARAMS.num_epochs, batch_size = HYPER_PARAMS.batch_size, shuffle=True, verbose=1, validation_data=(xval, yval), callbacks=[model_checkpoint,model_earlystop, model_history, model_board, save_checkpoint_gcs]) pd.DataFrame(model_history.history).to_csv(FMT_VALMODEL_HIST.format(PREFIX), index=False) copy_file_to_gcs(HYPER_PARAMS.job_dir, FMT_VALMODEL_HIST.format(PREFIX)) unet.save_weights(FMT_VALMODEL_LAST_PATH.format(PREFIX)) copy_file_to_gcs(HYPER_PARAMS.job_dir, FMT_VALMODEL_LAST_PATH.format(PREFIX))
def fit(self, env, nb_steps, action_repetition=1, callbacks=None, verbose=1, visualize=False, nb_max_start_steps=0, start_step_policy=None, log_interval=10000, nb_max_episode_steps=None, version=None, custom_env=False): """Trains the agent on the given environment. # Arguments env: (`Env` instance): Environment that the agent interacts with. See [Env](#env) for details. nb_steps (integer): Number of training steps to be performed. action_repetition (integer): Number of times the agent repeats the same action without observing the environment again. Setting this to a value > 1 can be useful if a single action only has a very small effect on the environment. callbacks (list of `keras.callbacks.Callback` or `rl.callbacks.Callback` instances): List of callbacks to apply during training. See [callbacks](/callbacks) for details. verbose (integer): 0 for no logging, 1 for interval logging (compare `log_interval`), 2 for episode logging visualize (boolean): If `True`, the environment is visualized during training. However, this is likely going to slow down training significantly and is thus intended to be a debugging instrument. nb_max_start_steps (integer): Number of maximum steps that the agent performs at the beginning of each episode using `start_step_policy`. Notice that this is an upper limit since the exact number of steps to be performed is sampled uniformly from [0, max_start_steps] at the beginning of each episode. start_step_policy (`lambda observation: action`): The policy to follow if `nb_max_start_steps` > 0. If set to `None`, a random action is performed. log_interval (integer): If `verbose` = 1, the number of steps that are considered to be an interval. nb_max_episode_steps (integer): Number of steps per episode that the agent performs before automatically resetting the environment. Set to `None` if each episode should run (potentially indefinitely) until the environment signals a terminal state. # Returns A `keras.callbacks.History` instance that recorded the entire training process. """ if not self.compiled: raise RuntimeError( 'Your tried to fit your agent but it hasn\'t been compiled yet. Please call `compile()` before `fit()`.' ) if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format( action_repetition)) self.training = True #self.stop_training = False # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 callbacks = [] if not callbacks else callbacks[:] if verbose == 1: callbacks += [TrainIntervalLogger(interval=log_interval)] elif verbose > 1: callbacks += [TrainEpisodeLogger()] if visualize: callbacks += [Visualizer()] history = History() # get the history class callbacks += [history] # Assign history to callback callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_steps': nb_steps, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_train_begin() callbacks.on_train_begin() episode = np.int16(0) self.step = np.int16(0) observation = None episode_reward = None episode_step = None #self.episode_step = None # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 did_abort = False # open workbook to store result workbook = xlwt.Workbook() sheet = workbook.add_sheet('DQN') sheet_step = workbook.add_sheet('step') try: while self.step < nb_steps: if observation is None: # start of a new episode callbacks.on_episode_begin(episode) episode_step = np.int16(0) #self.episode_step = np.int16(0) # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 episode_reward = np.float32(0) # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint( nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, reward, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, reward, done, info = self.processor.process_step( observation, reward, done, info) callbacks.on_action_end(action) if done: warnings.warn( 'Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.' .format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) break # At this point, we expect to be fully initialized. assert episode_reward is not None assert episode_step is not None #assert self.episode_step is not None # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 assert observation is not None # Run a single step. callbacks.on_step_begin(episode_step) #callbacks.on_step_begin(callbacks.on_step_begin(self.episode_step)) # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 # This is were all of the work happens. We first perceive and compute the action # (forward step) and then use the reward to improve (backward step). action = self.forward(observation) if self.processor is not None: action = self.processor.process_action(action) reward = np.float32(0) accumulated_info = {} done = False for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, done, info = env.step(action) # print(observation, r, done, info) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step( observation, r, done, info) for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value callbacks.on_action_end(action) reward += r if done: break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: #if nb_max_episode_steps and self.episode_step >= nb_max_episode_steps - 1: # Force a terminal state. done = True if (custom_env): metrics = self.backward(reward[0], terminal=done) # tran's version else: metrics = self.backward( reward, terminal=done) # for testing with dqn_cartpole episode_reward += reward if (custom_env): step_logs = { 'action': action, 'observation': observation, 'reward': reward[0], # tran's version 'metrics': metrics, 'episode': episode, 'info': accumulated_info, 'throughput': reward[1], } else: step_logs = { 'action': action, 'observation': observation, 'reward': reward, # for testing with dqn_cartpole 'metrics': metrics, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 #callbacks.on_step_end(self.episode_step, step_logs) # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 #self.episode_step += 1 # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 self.step += 1 if done: # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # This episode is finished, report and reset. if (custom_env): episode_logs = { 'episode_reward': episode_reward[0], # Only return the first value 'throughput': episode_reward[1], #'nb_episode_steps': episode_step, #'nb_steps': self.step, #'loss': history['loss'], } else: episode_logs = { 'episode_reward': episode_reward, # seems to return an array 'nb_episode_steps': episode_step, 'nb_steps': self.step, } print("Episode Number: ", episode) print("Episode Rewards: ", episode_reward) #print("Episode Logs", episode_logs) #print("Episode metrics", metrics) print(history.history.keys()) # print("History Loss", hist.history['loss']) # print("History Loss", hist.history['acc']) # print("History Loss", hist.history['val_loss']) # print("History Loss", hist.history['val_acc']) callbacks.on_episode_end(episode, episode_logs) #print("Episode Reward size is: ", len(episode_reward)) #print("Reward array size is: ", episode_reward) sheet.write(episode + 1, 0, str(episode)) sheet.write(episode + 1, 1, str(episode_reward[0])) sheet.write(episode + 1, 2, str(episode_reward[1])) #sheet.write(episode + 1, 3, str(episode_reward[2])) # for 2 #sheet.write(episode + 1, 4, str(episode_reward[3])) # for 3 #sheet.write(episode + 1, 5, str(episode_reward[4])) # for 4 episode += 1 observation = None #episode_step = None self.episode_step = None # https://github.com/keras-rl/keras-rl/pull/170/commits/73c073d773ade1ccf70bdcd8f96237473b206ed8 episode_reward = None except KeyboardInterrupt: # We catch keyboard interrupts here so that training can be be safely aborted. # This is so common that we've built this right into this function, which ensures that # the `on_train_end` method is properly called. did_abort = True callbacks.on_train_end(logs={'did_abort': did_abort}) self._on_train_end() file_name = 'result_v' + version + '.xls' # if (self.enable_double_dqn): # file_name = 'DDQN_' + file_name # if (self.enable_dueling_network): # file_name = 'Dueling_' + file_name workbook.save('../results/' + file_name) return history
model.add(Dense(512, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(30)) return model X_train, y_train = data_loader() epochs = 60 batch_size = 64 model = the_model() hist = History() checkpointer = ModelCheckpoint(filepath='checkpoint1.hdf5', verbose=1, save_best_only=True) model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) model_fit = model.fit(X_train, y_train, validation_split=0.2, epochs=epochs, batch_size=batch_size, callbacks=[checkpointer, hist],
def train_and_predict(): print('[{}] Loading and preprocessing train data...'.format(str(datetime.datetime.now()))) imgs_train, imgs_mask_train, train_ids = load_train_data() imgs_train = preprocess(imgs_train) imgs_mask_train = preprocess(imgs_mask_train) imgs_train = imgs_train.astype('float32') print('[{}] Creating validation set...'.format(str(datetime.datetime.now()))) # X_train = imgs_train # y_train = imgs_mask_train X_train, X_val, y_train, y_val = _train_val_split(imgs_train, imgs_mask_train) # print('[{}] Getting extra data...'.format(str(datetime.datetime.now()))) # extra_x, extra_y = extra_data() # # X_train = np.vstack([X_train, extra_x[:4000, :, :, :]]) # X_val = np.vstack([X_val, extra_x[4000:, :, :, :]]) datagen = image_generator_xy.ImageDataGenerator( featurewise_center=False, featurewise_std_normalization=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, # zoom_range=0.1, # # channel_shift_range=30, # shear_range=5, # # zca_whitening=True, horizontal_flip=True, vertical_flip=True ) mean = np.mean(X_train) # mean for data centering X_train -= mean X_train /= 255 # We can probably live without this. # X_test -= mean # X_test /= 255 # We can probably live without this. X_val -= mean X_val /= 255 # We can probably live without this. y_train = (y_train.astype(np.float32) / 255).astype(int).astype(float) # scale masks to [0, 1] # y_test = (y_test.astype(np.float32) / 255).astype(int).astype(float) # scale masks to [0, 1] y_val = (y_val.astype(np.float32) / 255).astype(int).astype(float) # scale masks to [0, 1] # y_train = np.vstack([y_train, extra_y[:4000, :, :, :]]).astype(int).astype(float) # y_val = np.vstack([y_val, extra_y[4000:, :, :, :]]).astype(int).astype(float) print print '[{}] Num train non zero masks...'.format(np.mean((np.mean(y_train, (2, 3)) > 0).astype(int).flatten())) print '[{}] Num val non zero masks...'.format(np.mean((np.mean(y_val, (2, 3)) > 0).astype(int).flatten())) # print '[{}] Num test non zero masks...'.format(np.mean((np.mean(y_test, (2, 3)) > 0).astype(int).flatten())) print('[{}] Creating and compiling model...'.format(str(datetime.datetime.now()))) # model = get_unet4() model = vgg_fcn() # model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss', save_best_only=True) print('[{}] Fitting generator...'.format(str(datetime.datetime.now()))) history = History() datagen.fit(X_train) print('[{}] Fitting model...'.format(str(datetime.datetime.now()))) batch_size = 16 nb_epoch = 20 model.compile(optimizer=Nadam(lr=1e-3), loss=dice_coef_loss, metrics=[dice_coef, dice_coef_spec, 'binary_crossentropy']) model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True), nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val), samples_per_epoch=len(X_train), callbacks=[history], # shuffle=True ) now = datetime.datetime.now() suffix = str(now.strftime("%Y-%m-%d-%H-%M")) save_model(model, "{batch}_{epoch}_{suffix}".format(batch=batch_size, epoch=nb_epoch, suffix=suffix)) print('[{data}] Saving model. Suffix = {suffix}'.format(data=str(datetime.datetime.now()), suffix=suffix)) model.compile(optimizer=Nadam(lr=1e-4), loss=dice_coef_loss, metrics=[dice_coef, dice_coef_spec, 'binary_crossentropy']) model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True), nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val), samples_per_epoch=len(X_train), callbacks=[history], # shuffle=True ) now = datetime.datetime.now() suffix = str(now.strftime("%Y-%m-%d-%H-%M")) save_model(model, "{batch}_{epoch}_{suffix}".format(batch=batch_size, epoch=nb_epoch, suffix=suffix)) print('[{data}] Saving model. Suffix = {suffix}'.format(data=str(datetime.datetime.now()), suffix=suffix)) model.compile(optimizer=Nadam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef, dice_coef_spec, 'binary_crossentropy']) model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True), nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val), samples_per_epoch=len(X_train), callbacks=[history], # shuffle=True ) now = datetime.datetime.now() suffix = str(now.strftime("%Y-%m-%d-%H-%M")) save_model(model, "{batch}_{epoch}_{suffix}".format(batch=batch_size, epoch=nb_epoch, suffix=suffix)) print('[{data}] Saving model. Suffix = {suffix}'.format(data=str(datetime.datetime.now()), suffix=suffix)) model.compile(optimizer=Nadam(lr=1e-6), loss=dice_coef_loss, metrics=[dice_coef, dice_coef_spec, 'binary_crossentropy']) model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True), nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val), samples_per_epoch=len(X_train), callbacks=[history], # shuffle=True ) now = datetime.datetime.now() suffix = str(now.strftime("%Y-%m-%d-%H-%M")) save_model(model, "{batch}_{epoch}_{suffix}".format(batch=batch_size, epoch=nb_epoch, suffix=suffix)) print print('[{}] Loading and preprocessing test data...'.format(str(datetime.datetime.now()))) imgs_test, imgs_id_test = load_test_data() imgs_test = preprocess(imgs_test) imgs_test = imgs_test.astype('float32') imgs_test -= mean imgs_test /= 255 print('[{}] Predicting masks on test data...'.format(str(datetime.datetime.now()))) imgs_mask_test = model.predict(imgs_test, verbose=1) np.save('masks/imgs_mask_test_{suffix}.npy'.format(suffix=suffix), imgs_mask_test) suffix = str(now.strftime("%Y-%m-%d-%H-%M")) print '[{}] Saving history'.format(str(datetime.datetime.now())) save_history(history, suffix) val_prediction = model.predict(X_val).astype(int).astype(float) print 'binarized_prediction on val = ', dice_coef_np(y_val, val_prediction)
def createModel(X_train, y_train, X_test, y_test, input_shape): ''' This creates a sequential model from the Keras Library to try a simple approach to creating DNNs This will not be needed for the TAs grading this project. The below Code is only for the 13 Layer Network Which is already trained and stored as an h5 file in the models directory. The configuration for every network is in models/configs/ This function will rely heavily on Keras -- specficially it's sequential model class. ''' #This is an example code for my Custom 10 Layer Network vgg13model = Sequential([ Conv2D(32, (3, 3), input_shape=input_shape, padding='same', activation='relu', data_format='channels_last'), Conv2D(32, (3, 3), activation='relu', padding='same'), Conv2D(32, (3, 3), activation='relu', padding='same'), MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.25), # # Conv2D(16, (3, 3), activation='relu', padding='same'), # # Conv2D(16, (3, 3), activation='relu', padding='same', ), # # Conv2D(16, (3, 3), activation='relu', padding='same', ), # # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Conv2D(6, (3, 3), activation='relu', padding='same', ), # Conv2D(6, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.25), # # Conv2D(8, (3, 3), activation='relu', padding='same', ), # Conv2D(8, (3, 3), activation='relu', padding='same', ), # Conv2D(8, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.5), Conv2D( 64, (3, 3), activation='relu', padding='same', ), Conv2D( 64, (3, 3), activation='relu', padding='same', ), Conv2D( 64, (3, 3), activation='relu', padding='same', ), Conv2D( 64, (3, 3), activation='relu', padding='same', ), MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), Flatten(), Dense(4096, activation='relu', name='fc1'), Dense(4096, activation='relu', name='fc2'), Dense(10, activation='softmax', name='predictions') ]) #This is example code for my vgg16 style implementation # vgg16model = Sequential([ # Conv2D(32, (3, 3), input_shape=input_shape, padding='same', activation='relu', data_format='channels_last'), # Conv2D(32, (3, 3), activation='relu', padding='same'), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.25), # # Conv2D(64, (3, 3), activation='relu', padding='same'), # Conv2D(64, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.5), # # Conv2D(128, (3, 3), activation='relu', padding='same', ), # Conv2D(128, (3, 3), activation='relu', padding='same', ), # Conv2D(64, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # # Dropout(.5), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # Conv2D(256, (3, 3), activation='relu', padding='same', ), # MaxPooling2D(pool_size=(2, 2), strides=(2, 2)), # Dropout(.5), # Flatten(), # Dense(256, activation='relu', name='fc1'), # Dense(256, activation='relu', name='fc2'), # Dense(10, activation='softmax', name='predictions') # ]) sgd = SGD(lr=0.15, decay=1e-6, momentum=0.9, nesterov=True) vgg13model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True, rotation_range=180, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, data_format='channels_last') print('METRICS NAMES') print(vgg13model.metrics_names) print('-' * 100) print('TRAINING MODEL') history = History() early_stop = keras.callbacks.EarlyStopping(monitor='loss', min_delta=0, patience=2, verbose=0, mode='auto') #model settings callbacks = [history, early_stop] epochs = 30 batch_size = 128 print('X_Train shape {}'.format(X_train.shape[0])) # # model_history = vgg13model.fit_generator( datagen.flow(X_train, y_train, batch_size=batch_size), steps_per_epoch=int(X_train.shape[0] / batch_size), verbose=1, epochs=epochs, callbacks=callbacks, shuffle=True, ) # model_history = vgg13model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, callbacks=callbacks) #saving model filename = 'models/vgg13_adam_datagen_' + str(epochs) + '_' + str( batch_size) vgg13model.save(filename) print('-' * 100) print('SCORING MODEL') score = vgg13model.evaluate(X_test, y_test, batch_size=64) print(vgg13model.metrics_names) print(score) return vgg13model, model_history, score
input_img = Input(shape=(83,)) model = Dense(64, activation='relu')(input_img) model = Dense(32, activation='relu')(model) model = Dense(16, activation='relu')(model) model = Dense(1, activation='relu')(model) model = Model(input_img, model) model.compile(loss=keras_loss.mse, optimizer = 'adam', metrics=['accuracy']) #, metrics=['accuracy'] return model seed = 7 np.random.seed(seed) scale = StandardScaler() #train_x = scale.fit_transform(train_x) #train_x = scale.fit_transform(train_x) train_history = History() #clf = KerasRegressor(build_fn=base_model, nb_epoch=100, batch_size=5,verbose=0, callbacks=[train_history], shuffle=True) dnn = base_model() dnn.fit(train_x,train_y,epochs=100, callbacks=[train_history], shuffle=True, validation_data=[val_x, val_y]) pred_dnn = dnn.predict(test_x) e_nn3 = test_y - pred_dnn ## line below throws an error #clf.score(test_y, res) plt.figure(figsize=(15,8)) plt.plot(np.arange(0, 100), train_history.history["loss"], label="train loss") plt.plot(np.arange(0, 100), train_history.history["val_loss"], label="val loss") plt.title("Neural network Loss") plt.xlabel("Epoch/Iteration") plt.ylabel("Loss") plt.legend(loc="upper right")
def lstmtest(allpiecelist): ## allpiecelist = ''.join(allpiecelist) #turn into a char list without space ## allpiecelist = removeSpaces(allpiecelist) #turn into a char list without space ## print(allpiecelist[1:200]) notelist = list(allpiecelist) print("length of the char: ",len(notelist)) #fix the random number seed to ensure the results are reproducible. np.random.seed(7) ## #change the 2d list to np array and flatten into 1d ## allpiecelist = flatten(allpiecelist) ## char = sort_and_deduplicate(allpiecelist) #normalize the dataset #create mapping of unique chars to integers sortedlist = sorted(list(set(notelist))) ## sortedlist.remove(" ") ## print(sortedlist) ## chars = sorted(list(set(notelist))) chars = sortedlist print("chars:" ,chars) #total piano note range encoding = {c: i for i, c in enumerate(chars)} decoding = {i: c for i, c in enumerate(chars)} ## char_to_int = dict((c, i) for i, c in enumerate(chars)) n_chars = len(notelist) n_vocab = len(chars) print("Our file contains {0} unique characters.".format(len(chars))) ## print ("Total Characters: ", n_chars) #overall notelist size ## print ("Total Vocab: ", n_vocab) #all the indentitical notes #convert the characters to integers using lookup table seq_length =150 #sentence length skip = 1 # -----? dataX = [] dataY = [] #chop the data by sequence length for i in range(0, len(allpiecelist) - seq_length, skip): seq_in = notelist[i:i + seq_length] seq_out = notelist[i + seq_length] dataX.append([encoding[char] for char in seq_in]) dataY.append(encoding[seq_out]) n_patterns = len(dataX) #number of sequences print("Sliced the input file into {0} sequenes of length {1}".format(n_patterns, seq_length)) #vectorize the x and y: ## #approach 1 # reshape X to be [samples, time steps, features] ## X = np.reshape(dataX, (n_patterns, seq_length, 1)) ## # normalize ## X = X / float(n_vocab) ## # one hot encode the output variable ## y = np_utils.to_categorical(dataY) ## #approach 2 print("len",len(chars)) print("vectorize x y ...") #slice the data into x and y with arbitrary overlapping sequences #the len of slice of the data, the predefined length, the total num of chars X = np.zeros((len(dataX), seq_length, len(chars)) ) #, dtype=np.bool) #filled with boolean falses y = np.zeros((len(dataX), len(chars)) ) #, dtype=np.bool) #fill the np array for i, sequence in enumerate(dataX): for t, encoded_char in enumerate(sequence): X[i, t, encoded_char] = 1 y[i, dataY[i]] = 1 #double check the vectorized data before training print("Sanity check y. Dimension: {0} # Sentences: {1} Characters in file: {2}".format(y.shape, n_patterns, len(chars))) print("Sanity check X. Dimension: {0} Sentence length: {1}".format(X.shape, seq_length)) ## print(X[1]) #define the lstm model #return either sequences (return_sequences=True) or a matrix. model = Sequential() model.add(LSTM(512, input_shape=(seq_length, len(chars)) )) #returning sequence model.add(Dropout(0.5)) #probaility #add different layers ## model.add(LSTM(256/len(chars), input_shape=(X.shape))) ## model.add(Dropout(0.3)) ## model.add(LSTM(256, input_shape=(seq_length, len(chars)))) #add the regularizers model.add(Dense(len(chars), activation='softmax', kernel_regularizer=keras.regularizers.l2(0.010))) model.compile(loss='categorical_crossentropy', optimizer='adam') #output the architecture in a different file so that it can be loaded seperately architecture = model.to_yaml() with open('model_regularizer_v2.yaml', 'a') as model_file: model_file.write(architecture) #define the checkpoint filepath="weights-{epoch:02d}-{loss:.4f}.hdf5" #verbose = 1 ? 0 checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min') callbacks_list = [checkpoint] #fittttt the model!!!! history = History() #just for debugging epochsize = 2 history = model.fit(X, y, epochs=epochsize, batch_size=254, callbacks=callbacks_list) #for debugging ## layer = Dense(Dense(y.shape[1], activation='softmax')) ## print("layer: ", layer.get_weights()) ## print("history: ",history.history.keys()) ## print("loss: ",history.history['loss']) lossnum = history.history['loss'][0] lossnum = "%.4f" % lossnum epochsize = "{:02d}".format(epochsize)
opt = keras.optimizers.Adam(lr=lr) #------------------------Model_ Compile-------------------------------------- # model = multi_gpu_model(model, gpus=gpus) # for multi-gpu power 9 model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) #-------------------------------- Define checkpoint and History----------- checkpoint = ModelCheckpoint(filename + '_' + h5_name, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) callbacks_list = [checkpoint, History()] # -----------------------------Training-------------------------------------------- hist = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val), callbacks=callbacks_list, verbose=1) # --------------------------Logs of loss and acc--------------------------------- train_loss = hist.history['loss'] val_loss = hist.history['val_loss'] train_acc = hist.history['acc'] val_acc = hist.history['val_acc'] xc = range(epochs)
def training_round(round_name, n_of_epochs, markers = [], context = {'loss_hist': []}): print('=' * 50) print('Preparing the dataset...') X, y, pos_seq_count = prepare_dataset(markers=markers) print('Dataset size:', len(X), 'out of', pos_seq_count, 'possible sequences') history = History() loss_hist = context['loss_hist'] for iteration in range(1, n_of_epochs + 1): print() print('-' * 50) print('Iteration', iteration, 'of', n_of_epochs, '[' + round_name + ']') #model.fit_generator(generator=batch_generator(X, y, batch_size), samples_per_epoch=X.shape[0], nb_epoch=1, verbose=2, callbacks=[history]) model.fit_generator(generator=batch_generator(X, y, batch_size), samples_per_epoch=len(X), nb_epoch=1, verbose=2, callbacks=[history]) start_index = random.randint(0, len(text_as_idx) - input_len - 1) initial_sentence = text_as_idx[start_index: start_index + input_len] print() print('---- max:') sentence = list(initial_sentence) sys.stdout.write(idx_arr2str(sentence) + ' ===> ') sys.stdout.flush() most_probable = [] most_probable2 = [] for i in range(sample_output_len): x = extract_input([sentence]) preds = model.predict(x, verbose=0)[0] generated_w_idx = np.argmax(preds) sys.stdout.write(' ' + idx_to_word[generated_w_idx]) sys.stdout.flush() sentence = np.append(sentence[1:], generated_w_idx) most_probable.append([ (idx_to_word[p[0]], float('%.4f'%(p[1]))) for p in sorted(enumerate(preds), key=lambda p: p[1], reverse=1)[:candidate_out_size] ]) most_probable2.extend([ idx_to_word[p[0]] for p in sorted(enumerate(preds), key=lambda p: p[1], reverse=1)[:candidate_out_size*4] ]) print() for mp in most_probable: print(mp) print() print(' '.join(most_probable2)) for temperature in [0.2, 0.5, 1.0, 1.2]: print('---- temperature: ', temperature) sentence = list(initial_sentence) sys.stdout.write(idx_arr2str(sentence) + ' ===> ') sys.stdout.flush() generated = '' for i in range(sample_output_len): x = extract_input([sentence]) preds = model.predict(x, verbose=0)[0] generated_w_idx = sample(preds, temperature) sys.stdout.write(' ' + idx_to_word[generated_w_idx]) sys.stdout.flush() sentence = np.append(sentence[1:], generated_w_idx) generated += ' ' + idx_to_word[generated_w_idx] print() print() if draw_loss_graph: # draw the loss graph loss_hist.append(history.history['loss'][0]) plt.figure(figsize=(20, 6)) plt.plot(loss_hist) plt.savefig('loss.png', dpi=150) plt.close() if save_the_model: #1 == iteration or 0 == iteration%100: # save the model model_files_prefix = round_name + '-iter' model.save(model_files_prefix + '.h5') with open(model_files_prefix + '-vocab.pickle', "wb" ) as f: pickle.dump(vocab, f) return { 'loss_hist': loss_hist }
def fit(self, x, y, batch_size=None, nsteps=None, epochs=1, verbose=1, callbacks=None, validation_data=None): assert self.is_compiled, "Must compile model first" assert epochs > 0 x = x if type(x) is list else [x] y = y if type(y) is list else [y] if nsteps is None: total_len = len(y[0]) if type(y) is list else len(y) nsteps = total_len // batch_size # BaseLogger should always be the first metric since it computes the stats on epoch end base_logger = BaseLogger( stateful_metrics=["val_%s" % m for m in self.metrics_name] + ['val_loss', 'size']) base_logger_params = {'metrics': ['loss'] + self.metrics_name} if validation_data: base_logger_params['metrics'] += [ 'val_%s' % m for m in base_logger_params['metrics'] ] base_logger.set_params(base_logger_params) hist = History() if callbacks is None: callbacks = [base_logger] + [hist] elif type(callbacks) is list: callbacks = [base_logger] + callbacks + [hist] else: callbacks = [base_logger] + [callbacks] + [hist] callback_list = CallbackList(callbacks=callbacks) callback_list.set_model(self) callback_list.on_train_begin() self.callbacks = callback_list for epoch in range(epochs): g = batchify(x, y, batch_size) if batch_size else None t = trange(nsteps) if verbose == 1 else range(nsteps) callback_list.on_epoch_begin(epoch) for it in t: x_, y_ = next(g) if g else (None, None) batch_logs = self.train_on_batch(x_, y_) callback_list.on_batch_end(it, batch_logs) curr_loss = base_logger.totals['loss'] / base_logger.seen if verbose == 1: t.set_postfix(loss="%.4f" % curr_loss) if verbose == 2: if it % 1000 == 0: print("%s %i/%i, loss=%.5f" % (datetime.datetime.now().strftime("%H:%M:%S"), it, nsteps, curr_loss), flush=True) if validation_data: val_logs = self.evaluate(validation_data[0], validation_data[1]) base_logger.on_batch_end(None, val_logs) epoch_logs = {} callback_list.on_epoch_end(epoch=epoch, logs=epoch_logs) if verbose: if validation_data: to_print = ['loss'] + self.metrics_name + ['val_loss'] + [ 'val_%s' % m for m in self.metrics_name ] else: to_print = ['loss'] + self.metrics_name prog = ", ".join([ "%s=%.4f" % (name, hist.history[name][-1]) for name in to_print ]) print("Epoch %i, %s" % (epoch, prog), flush=True) if self.stop_training: break return hist.history
model.add(Dense(nb_class)) model.add(Activation('softmax')) ada = Adadelta(lr=0.1, rho=0.95, epsilon=1e-08) model.compile(loss='categorical_crossentropy', optimizer=ada, metrics=['accuracy']) model.summary() callbacks = [ EarlyStopping(monitor='val_loss', patience=patience, verbose=0), ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, verbose=1), History() ] datagen = ImageDataGenerator(width_shift_range=0.5, height_shift_range=0.5, rotation_range=40, zoom_range=0.2, horizontal_flip=True, vertical_flip=False) datagen.fit(train_feature) history = model.fit_generator(datagen.flow(x=train_feature, y=train_label, batch_size=batch_size), steps_per_epoch=int(
def _fit(self, adata, condition_key, train_size=0.8, n_epochs=300, batch_size=512, early_stop_limit=10, lr_reducer=7, save=True, retrain=True, verbose=3): train_adata, valid_adata = train_test_split(adata, train_size) if self.gene_names is None: self.gene_names = train_adata.var_names.tolist() else: if set(self.gene_names).issubset(set(train_adata.var_names)): train_adata = train_adata[:, self.gene_names] else: raise Exception("set of gene names in train adata are inconsistent with class' gene_names") if set(self.gene_names).issubset(set(valid_adata.var_names)): valid_adata = valid_adata[:, self.gene_names] else: raise Exception("set of gene names in valid adata are inconsistent with class' gene_names") train_expr = train_adata.X.A if sparse.issparse(train_adata.X) else train_adata.X valid_expr = valid_adata.X.A if sparse.issparse(valid_adata.X) else valid_adata.X train_conditions_encoded, self.condition_encoder = label_encoder(train_adata, le=self.condition_encoder, condition_key=condition_key) valid_conditions_encoded, self.condition_encoder = label_encoder(valid_adata, le=self.condition_encoder, condition_key=condition_key) if not retrain and os.path.exists(os.path.join(self.model_path, f"{self.model_name}.h5")): self.restore_model_weights() return callbacks = [ History(), ] if verbose > 2: callbacks.append( LambdaCallback(on_epoch_end=lambda epoch, logs: print_progress(epoch, logs, n_epochs))) fit_verbose = 0 else: fit_verbose = verbose if early_stop_limit > 0: callbacks.append(EarlyStopping(patience=early_stop_limit, monitor='val_loss')) if lr_reducer > 0: callbacks.append(ReduceLROnPlateau(monitor='val_loss', patience=lr_reducer)) train_conditions_onehot = to_categorical(train_conditions_encoded, num_classes=self.n_conditions) valid_conditions_onehot = to_categorical(valid_conditions_encoded, num_classes=self.n_conditions) x_train = [train_expr, train_conditions_onehot, train_conditions_onehot] x_valid = [valid_expr, valid_conditions_onehot, valid_conditions_onehot] y_train = [train_expr, train_conditions_encoded] y_valid = [valid_expr, valid_conditions_encoded] self.cvae_model.fit(x=x_train, y=y_train, validation_data=(x_valid, y_valid), epochs=n_epochs, batch_size=batch_size, verbose=fit_verbose, callbacks=callbacks, ) if save: self.save(make_dir=True)
def fit(self, env, nb_steps, action_repetition=1, callbacks=None, verbose=1, visualize=False, nb_max_start_steps=0, start_step_policy=None, log_interval=10000, nb_max_episode_steps=None): """Trains the agent on the given environment. # Arguments env: (`Env` instance): Environment that the agent interacts with. See [Env](#env) for details. nb_steps (integer): Number of training steps to be performed. action_repetition (integer): Number of times the agent repeats the same action without observing the environment again. Setting this to a value > 1 can be useful if a single action only has a very small effect on the environment. callbacks (list of `keras.callbacks.Callback` or `rl.callbacks.Callback` instances): List of callbacks to apply during training. See [callbacks](/callbacks) for details. verbose (integer): 0 for no logging, 1 for interval logging (compare `log_interval`), 2 for episode logging visualize (boolean): If `True`, the environment is visualized during training. However, this is likely going to slow down training significantly and is thus intended to be a debugging instrument. nb_max_start_steps (integer): Number of maximum steps that the agent performs at the beginning of each episode using `start_step_policy`. Notice that this is an upper limit since the exact number of steps to be performed is sampled uniformly from [0, max_start_steps] at the beginning of each episode. start_step_policy (`lambda observation: action`): The policy to follow if `nb_max_start_steps` > 0. If set to `None`, a random action is performed. log_interval (integer): If `verbose` = 1, the number of steps that are considered to be an interval. nb_max_episode_steps (integer): Number of steps per episode that the agent performs before automatically resetting the environment. Set to `None` if each episode should run (potentially indefinitely) until the environment signals a terminal state. # Returns A `keras.callbacks.History` instance that recorded the entire training process. """ if not self.compiled: raise RuntimeError( 'Your tried to fit your agent but it hasn\'t been compiled yet. Please call `compile()` before `fit()`.' ) if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format( action_repetition)) self.training = True callbacks = [] if not callbacks else callbacks[:] if verbose == 1: callbacks += [TrainIntervalLogger(interval=log_interval)] elif verbose > 1: callbacks += [TrainEpisodeLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_steps': nb_steps, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_train_begin() callbacks.on_train_begin() episode = np.int16(0) self.step = np.int16(0) observation = None episode_reward = None episode_step = None did_abort = False try: while self.step < nb_steps: if observation is None: # start of a new episode callbacks.on_episode_begin(episode) episode_step = np.int16(0) episode_reward = np.float32(0) # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint( nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, reward, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, reward, done, info = self.processor.process_step( observation, reward, done, info) callbacks.on_action_end(action) if done: warnings.warn( 'Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.' .format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) break # At this point, we expect to be fully initialized. assert episode_reward is not None assert episode_step is not None assert observation is not None # Run a single step. callbacks.on_step_begin(episode_step) # This is were all of the work happens. We first perceive and compute the action # (forward step) and then use the reward to improve (backward step). action = self.forward(observation) if self.processor is not None: action = self.processor.process_action(action) reward = np.float32(0) accumulated_info = {} done = False for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step( observation, r, done, info) for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value callbacks.on_action_end(action) reward += r if done: break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: # Force a terminal state. done = True metrics = self.backward(reward, terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward, 'metrics': metrics, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 self.step += 1 if done: # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # This episode is finished, report and reset. episode_logs = { 'episode_reward': episode_reward, 'nb_episode_steps': episode_step, 'nb_steps': self.step, } callbacks.on_episode_end(episode, episode_logs) episode += 1 observation = None episode_step = None episode_reward = None except KeyboardInterrupt: # We catch keyboard interrupts here so that training can be be safely aborted. # This is so common that we've built this right into this function, which ensures that # the `on_train_end` method is properly called. did_abort = True callbacks.on_train_end(logs={'did_abort': did_abort}) self._on_train_end() return history
def process_neural_network(self, max_len, g): self.num_params = 7 self.nn = None models = list() l = list() params = self.get_best_models_params_CNN() class_weight_val = class_weight.compute_class_weight( 'balanced', np.unique(self.y_all), self.y_all) class_weight_dictionary = { 0: class_weight_val[0], 1: class_weight_val[1], 2: class_weight_val[2] } for combination in params: # self.nn = TweetSentimentInceptionV2_3x3(max_len, g) self.nn = TweetSentimentInceptionV2_5x5(max_len, g) # self.nn = TweetSentimentInceptionV2_5x5_Multi(max_len, g) # self.nn = TweetSentimentInceptionV2_3x3_Multi(max_len, g) file_name = str( self.route_files) + "/model" + str(combination).replace( " ", "") + ".txt" log = open(file_name, "a+") start_time_comb = datetime.now() log.write("Start time: " + str(start_time_comb) + "\n") log.write("COMBINATION: " + str(combination) + "\n") l = list(combination) self.nn.build(filters=l[3], dropout=l[4], dense_units=l[5]) self.nn.summary() # Assign the parameters agree the optimizer to use params_compile = {} if l[6] == 'RMSPROP': rmsprop = RMSprop(lr=l[0], rho=0.9, epsilon=1e-06) params_compile['optimizer'] = rmsprop elif l[6] == 'ADAM': adam = Adam(lr=l[0], beta_1=0.9, beta_2=0.999) params_compile['optimizer'] = adam elif l[6] == 'ADADELTA': adaDelta = Adadelta(lr=l[0], rho=0.95, epsilon=1e-08, decay=0.0) params_compile['optimizer'] = adaDelta else: print( "OPTIMIZADOR: El optimizador a crear no esta en lista...") self.nn.compile( loss="categorical_crossentropy", metrics=['accuracy', precision, recall, f1, fprate], **params_compile) history = History() self.nn.fit(self.x_train, self.y_train, epochs=l[1], batch_size=l[2], callbacks=[history], class_weight=class_weight_dictionary) predicted = self.nn.predict(self.x_valid) predicted = np.argmax(predicted, axis=1) # Getting metrics acc = accuracy(self.y_valid, predicted) c_matrix = confusion_matrix(self.y_valid, predicted) track = ("Confusion matrix : \n{}\n" "Model accuracy: {}\n").format(c_matrix, acc) prec_1, recall_1, f1_1, spec_1, track = self.calculate_cm_metrics( c_matrix, track) model = [ acc, # Accuracy prec_1, # Precision recall_1, # Recall f1_1, # F1 Score spec_1, # Specificity file_name, 'NO' ] # SAVE MODEL json_route = self.route_files + "/model" + str( combination).replace(" ", "") + ".json" h5_route = self.route_files + "/model" + str(combination).replace( " ", "") + ".h5" self.nn.save_model(json_route, h5_route) print("Saved model to disk") models.append(model) models = sorted(models, key=itemgetter(3, 2, 1)) KerasBack.clear_session() log.write(track) log.write(("\nExecution time: {} minutes".format( ((datetime.now() - start_time_comb).total_seconds()) / 60))) log.write(("\nFinish time: " + str(datetime.now()))) log.close() return models
def train_coarse(self): # Input: #TODO check and enforce self.config['IMG_ROWS'], self.config['IMG_COLS'] are even inputs = Input(shape=(int(self.config['IMG_ROWS'] / 2), int(self.config['IMG_COLS'] / 2), 3)) # Coarse 1: # 11x11 conv, 4 stride, ReLU activation, 2x2 pool coarse_1 = Convolution2D(96, (11, 11), strides=(4, 4), padding='same', kernel_initializer='uniform', input_shape=(1, self.config['IMG_ROWS'] / 2, self.config['IMG_COLS'] / 2), name='coarse_1')(inputs) coarse_1 = Activation('relu')(coarse_1) coarse_1 = MaxPooling2D(pool_size=(2, 2))(coarse_1) # Coarse 2: # 5x5 conv, 1 stride, ReLU activation, 2x2 pool coarse_2 = Convolution2D(256, (5, 5), padding='same', kernel_initializer='uniform', name='coarse_2')(coarse_1) coarse_2 = Activation('relu')(coarse_2) coarse_2 = MaxPooling2D(pool_size=(2, 2))(coarse_2) # Coarse 3: # 3x3 conv, 1 stride, ReLU activation, no pool coarse_3 = Convolution2D(384, (3, 3), padding='same', kernel_initializer='uniform', name='coarse_3')(coarse_2) coarse_3 = Activation('relu')(coarse_3) # Coarse 4: # 3x3 conv, 1 stride, ReLU activation, no pool coarse_4 = Convolution2D(384, (3, 3), padding='same', kernel_initializer='uniform', name='coarse_4')(coarse_3) coarse_4 = Activation('relu')(coarse_4) # Coarse 5: # 3x3 conv, 1 stride, ReLU activation, 2x2 pool? coarse_5 = Convolution2D(256, (3, 3), padding='same', kernel_initializer='uniform', name='coarse_5')(coarse_4) coarse_5 = Activation('relu')(coarse_5) coarse_5 = MaxPooling2D(pool_size=(2, 2))(coarse_5) # Coarse 6: # Fully-connected, ReLU activation, followed by dropout coarse_6 = Flatten(name='coarse_6')(coarse_5) coarse_6 = Dense(4096, kernel_initializer='uniform')(coarse_6) coarse_6 = Activation('relu')(coarse_6) coarse_6 = Dropout(0.5)(coarse_6) # Coarse 7: # Fully-connected, linear activation coarse_7 = Dense((int(self.config['IMG_ROWS'] / 8)) * (int(self.config['IMG_COLS'] / 8)), kernel_initializer='uniform', name='coarse_7')(coarse_6) #XXX coarse_7 = Activation('linear')(coarse_7) coarse_7 = Reshape((int(self.config['IMG_ROWS'] / 8), int(self.config['IMG_COLS'] / 8)))(coarse_7) #XXX # compile the model #TODO compile model once and save (separate script) print('### COMPILING MODEL ###') model = Model(input=inputs, output=coarse_7) model.compile(loss=self.scale_invariant_error, optimizer=SGD(lr=self.config['LEARNING_RATE'], momentum=self.config['MOMENTUM']), metrics=['accuracy']) model.summary() # save the model architecture to file print('### SAVING MODEL ARCHITECTURE ###') modelDir = dateTimeStr os.mkdir(os.path.join(self.config['OUTDIR'], modelDir)) modelFile = os.path.join( self.config['OUTDIR'], modelDir, 'depth_coarse_model_{}.json'.format(dateTimeStr)) print(model.to_json(), file=open(modelFile, 'w')) # load and preprocess the data print('### LOADING DATA ###') X_train, Y_train = loadData( os.path.join(self.config['DATA_DIR'], 'train/')) X_test, Y_test = loadData( os.path.join(self.config['DATA_DIR'], 'test/')) print('X_train shape:', X_train.shape) print('Y_train shape:', Y_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # train the model #TODO use validation_split instead of using separate (test) data print('### TRAINING ###') history_cb = History() checkpointFile = os.path.join( self.config['OUTDIR'], modelDir, 'coarse-weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5') checkpoint_cb = ModelCheckpoint(filepath=checkpointFile, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True, mode='auto') model.fit(X_train, Y_train, epochs=self.config['EPOCHS'], batch_size=self.config['BATCH_SIZE'], verbose=1, validation_split=0.2, callbacks=[history_cb, checkpoint_cb]) histFile = os.path.join(self.config['OUTDIR'], modelDir, 'depth_coarse_hist_{}.h5'.format(dateTimeStr)) # save the model weights to file print('### SAVING TRAINED MODEL ###') print(history_cb.history, file=open(histFile, 'w')) weightsFile = os.path.join( self.config['OUTDIR'], modelDir, 'depth_coarse_weights_{}.h5'.format(dateTimeStr)) model.save_weights(weightsFile) # evaluate the trained model print('sleeping for 5 seconds...') time.sleep(5) print('### LOADING THE MODEL WEIGHTS ###') model_json = open(modelFile, 'r').read() model2 = model_from_json(model_json) model2.load_weights(weightsFile) model2.compile(loss=self.scale_invariant_error, optimizer=SGD(lr=self.config['LEARNING_RATE'], momentum=self.config['MOMENTUM']), metrics=['accuracy']) # evaluate the model print('### EVALUATING ###') score = model2.evaluate(X_test, Y_test, verbose=1) print('Test score:', score[0]) print('Test accuracy:', score[1])
def fit_generator(self, generator, epochs=1, validation_data=None, callbacks=None, verbose=True): method = self._model.optimizer.method x0 = self._collect_weights() history = History() _callbacks = [BaseLogger(stateful_metrics=self._model.metrics_names)] _callbacks += (callbacks or []) + [history] callback_list = CallbackList(_callbacks) callback_list.set_model(self._model) callback_list.set_params({ 'epochs': epochs, 'verbose': False, 'metrics': list(self._model.metrics_names), }) state = { 'epoch': 0, 'verbose': verbose, 'callbacks': callback_list, 'in_epoch': False, 'epoch_logs': {}, } min_options = { 'maxiter': epochs, 'maxfun': epochs * 10, 'maxcor': 50, 'maxls': 50, 'ftol': np.finfo(float).eps, 'gtol': 1e-10, 'eps': 1e-8, } val_generator = None if validation_data is not None: if isinstance(validation_data, keras.utils.Sequence): val_generator = validation_data elif isinstance(validation_data, tuple) and len(validation_data) == 2: val_generator = GeneratorWrapper(*validation_data) def on_iteration_end(xk): cb = state['callbacks'] if val_generator is not None: self._validate(xk, val_generator, state) cb.on_epoch_end(state['epoch'], state['epoch_logs']) # if state['verbose']: # epoch_logs = state['epoch_logs'] # print('epoch: ', state['epoch'], # ', '.join([' {0}: {1:.3e}'.format(k, v) for k, v in epoch_logs.items()])) state['epoch'] += 1 state['in_epoch'] = False state['epoch_logs'] = {} callback_list.on_train_begin() result = minimize(self._fun_generator, x0, method=method, jac=True, options=min_options, callback=on_iteration_end, args=(generator, state)) self._update_weights(result['x']) callback_list.on_train_end() return history
def run(self): if self.check_parameters() == False: raise IndexError( 'Parameters for LSTM is wrong, check out_class_type') ################################################################################ self.paras.save_folder = self.get_save_directory() print('Save Directory: ', self.paras.save_folder) ################################################################################ featureDropForTraining = ['label'] # get data df, df_valid, df_lately, df_all = self.GetStockData_PriceVolume() print('df len:', len(df)) print('df_valid len:', len(df_valid)) print('df_lately len:', len(df_lately)) print('df_all len:', len(df_all)) # preprocessing X, y, scaler = self.preprocessing_data(df, featureDropForTraining, with_label_proc=True) X_valid, y_valid, scaler_valid = self.preprocessing_data( df_valid, featureDropForTraining, with_label_proc=True) X_lately, y_lately, scaler_lately = self.preprocessing_data( df_lately, featureDropForTraining, with_label_proc=False) # cross validation X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) print('Train shape X:', X_train.shape, ',y:', y_train.shape) print('Test shape X:', X_test.shape, ',y:', y_test.shape) # reshape input data to LSTM model X, y = self.reshape_input(X, y) X_train, y_train = self.reshape_input(X_train, y_train) X_test, y_test = self.reshape_input(X_test, y_test) X_valid, y_valid = self.reshape_input(X_valid, y_valid) X_lately, y_lately = self.reshape_input(X_lately, y_lately) print('After reshape X_train shape:', X_train.shape) print('After reshape y_train shape:', y_train.shape) # build LSTM model history = History() model_lstm = self.build_LSTM_model() model_lstm.fit( X_train, y_train, batch_size=self.paras.batch_size, nb_epoch=self.paras.epoch, validation_split=self.paras.validation_split, # validation_data = (X_known_lately, y_known_lately), callbacks=[history], verbose=1) # save model self.save_training_model(model_lstm, 'lstm_model') # validation test + known lately data print(' ############## validation on test data ############## ') mse_test, tmp, tmp2 = self.LSTM_model_predict(model_lstm, X_test, y_test) print( ' ############## validation on train/test lately data ############## ' ) mse_traintest, tmp, tmp2 = self.LSTM_model_predict( model_lstm, X[-self.paras.valid_len:], y[-self.paras.valid_len:]) print(' ############## validation on valid data ############## ') mse_known_lately, df_all.loc[df_valid.index, 'actual'], df_all.loc[ df_valid.index, 'pred'] = self.LSTM_model_predict(model_lstm, X_valid, y_valid) # predict lately data print(' ############## validation on lately data ############## ') mse_lately, df_all.loc[df_lately.index, 'actual'], df_all.loc[ df_lately.index, 'pred'] = self.LSTM_model_predict(model_lstm, X_lately, y_lately) # rewrite data frame and save / update df_all = self.save_data_frame_mse(df_all, mses=[mse_test, mse_known_lately]) self.df = df_all # plot training loss/ validation loss self.plot_training_curve(history) pd.set_option('display.max_rows', None) print(df_all[-(self.paras.pred_len + self.paras.valid_len):])
def train(model, model_path): EPOCHS = 100 # should be 10 or bigger number BS = 40 if os.path.isfile(base_model): print("load last weight from:{}".format(base_model)) model.load_weights(base_model) model_checkpoint = ModelCheckpoint(model_path, monitor='val_jaccard_coef_int', save_best_only=False) # model_checkpoint = ModelCheckpoint( # model_save_path, # monitor='val_jaccard_coef_int', # save_best_only=True, # mode='max') model_earlystop = EarlyStopping(monitor='val_jaccard_coef_int', patience=6, verbose=0, mode='max') """自动调整学习率""" model_reduceLR = ReduceLROnPlateau(monitor='val_jaccard_coef_int', factor=0.1, patience=3, verbose=0, mode='max', epsilon=0.0001, cooldown=0, min_lr=0) model_history = History() callable = [ model_checkpoint, model_earlystop, model_reduceLR, model_history ] # callable = [model_checkpoint, model_reduceLR, model_history] # callable = [model_checkpoint,model_earlystop, model_history] train_set, val_set = get_train_val() train_numb = len(train_set) valid_numb = len(val_set) print("the number of train data is", train_numb) print("the number of val data is", valid_numb) H = model.fit_generator(generator=generateData(BS, train_set), steps_per_epoch=train_numb // BS, epochs=EPOCHS, verbose=1, validation_data=generateValidData(BS, val_set), validation_steps=valid_numb // BS, callbacks=callable, max_q_size=1) #plot the training loss and accuracy plt.style.use("ggplot") plt.figure() N = EPOCHS plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, N), H.history["acc"], label="train_acc") plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc") plt.plot(np.arange(0, N), H.history["jaccard_coef_int"], label="train_jaccard_coef_int") plt.plot(np.arange(0, N), H.history["val_jaccard_coef_int"], label="val_jaccard_coef_int") plt.title("Training Loss and Accuracy on U-Net Satellite Seg") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend(loc="lower left") fig_train_acc = ''.join([ '../../data/models/train_acc_', dict_network[FLAG_USING_NETWORK], '_', dict_target[FLAG_TARGET_CLASS], '_jaccard.png' ]) plt.savefig(fig_train_acc)
def __init__(self, stop_patience=10, lr_factor=0.5, lr_patience=1, lr_epsilon=0.001, lr_cooldown=4, lr_minimum=1e-5, outputDir=''): self.nl_begin = newline_callbacks_begin(outputDir) self.nl_end = newline_callbacks_end() self.stopping = EarlyStopping(monitor='val_loss', patience=stop_patience, verbose=1, mode='min') self.reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=lr_factor, patience=lr_patience, mode='min', verbose=1, epsilon=lr_epsilon, cooldown=lr_cooldown, min_lr=lr_minimum) self.modelbestcheck = ModelCheckpoint(outputDir + "/KERAS_check_best_model.h5", monitor='val_loss', verbose=1, save_best_only=True) self.modelbestcheckweights = ModelCheckpoint( outputDir + "/KERAS_check_best_model_weights.h5", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True) self.modelcheckperiod = ModelCheckpoint( outputDir + "/KERAS_check_model_epoch{epoch:02d}.h5", verbose=1, period=10) self.modelcheck = ModelCheckpoint(outputDir + "/KERAS_check_model_last.h5", verbose=1) self.modelcheckweights = ModelCheckpoint( outputDir + "/KERAS_check_model_last_weights.h5", verbose=1, save_weights_only=True) self.tb = TensorBoard(log_dir=outputDir + '/logs') self.history = History() self.timer = Losstimer() self.callbacks = [ self.nl_begin, self.modelbestcheck, self.modelbestcheckweights, self.modelcheck, self.modelcheckweights, self.modelcheckperiod, self.reduce_lr, self.stopping, self.nl_end, self.tb, self.history, self.timer ]
def training(model_name, dataset, learning_rate, num_epochs): features = [] #Images data = [] #Coordinates history = History() #Deleting already existing model if Path(model_name).is_file(): print("[INFO] Deleting previous model...") os.remove(model_name) # Loading labels print("[INFO] Loading labels...") samples = json.load(open(dataset + 'labels.json')) labels = list(samples.keys()) # Storing dataset in variables print("[INFO] Storing data...") for (i, label) in enumerate(labels): image = cv2.imread(dataset + label) features.append(util.image_to_feature_vector(image)) data.append(np.asarray(samples[label]).flatten()[0:4]) if i > 0 and i % 1000 == 0: print("[INFO] processed {}/{}".format(i, len(labels))) (trainData, testData, trainLabels, testLabels) = train_test_split(features, data, test_size=0.25, random_state=42) # MODEL model = main_model() # TRAINING print("[INFO] compiling model...") #sgd = SGD(lr=learning_rate, momentum=0.9, nesterov=True) #adam = Adam(lr = learning_rate) model.compile(loss='mean_squared_error', optimizer='adam', metrics=["accuracy"]) model.summary() model.fit(np.array(trainData), np.array(trainLabels), epochs=num_epochs, batch_size=4, verbose=1, callbacks=[history]) # TESTING print("[INFO] evaluating on testing set...") (loss, accuracy) = model.evaluate(np.array(testData), np.array(testLabels), batch_size=128, verbose=1) print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100)) # SAVING print("[INFO] dumping architecture and weights to file...") model.save(model_name) save_history(history)
def fit_dataset(self, dataset, steps_per_epoch=None, batch_size=32, epochs=1, verbose=1, callbacks=None): """Train the model on the given dataset for a given number of epochs. Arguments --------- dataset: Instance of `BaseDataset` that provides the data to train on. steps_per_epoch: int or None, number of gradient updates before considering an epoch has passed. If None it is set to be `len(dataset.train_data) / batch_size`. batch_size: int, number of samples per gradient update epochs: int, number of times to iterate `steps_per_epoch` times verbose: {0, >0}, whether to employ the progress bar Keras callback or not callbacks: list of Keras callbacks to be called during training """ # Set steps_per_epoch properly if steps_per_epoch is None: steps_per_epoch = len(dataset.train_data) / batch_size # Create the callbacks list self.history = History() callbacks = [BaseLogger()] + (callbacks or []) + [self.history] if verbose > 0: callbacks += [ProgbarLogger(count_mode="steps")] callbacks = CallbackList(callbacks) #TODO: Should we be making it possible to call back a different model # than self.model.model? callbacks.set_model(self.model.model) callbacks.set_params({ "epochs": epochs, "steps": steps_per_epoch, "verbose": verbose, "do_validation": len(dataset.test_data) > 0, "metrics": self._get_metric_names() + ["val_" + n for n in self._get_metric_names()] }) # Create the sampler sampler = self.sampler(dataset) # Start the training loop epoch = 0 self.model.model.stop_training = False callbacks.on_train_begin() while epoch < epochs: callbacks.on_epoch_begin(epoch) for step in range(steps_per_epoch): batch_logs = {"batch": step, "size": batch_size} callbacks.on_batch_begin(step, batch_logs) # Importance sampling is done here idxs, (x, y), w = sampler.sample(batch_size) # Train on the sampled data loss, metrics, scores = self.model.train_batch(x, y, w) # Update the sampler sampler.update(idxs, scores) values = map(lambda x: x.mean(), [loss] + metrics) for l, o in zip(self._get_metric_names(), values): batch_logs[l] = o callbacks.on_batch_end(step, batch_logs) # Evaluate now that an epoch passed epoch_logs = {} if len(dataset.test_data) > 0: val = self.model.evaluate(*dataset.test_data[:], batch_size=batch_size) epoch_logs = { "val_" + l: o for l, o in zip(self._get_metric_names(), val) } callbacks.on_epoch_end(epoch, epoch_logs) if self.model.model.stop_training: break epoch += 1 callbacks.on_train_end() return self.history
def train(): X_train, y_train = get_train_data() X_test, y_test = get_valid_data() # data generater train_gen = ImageDataGenerator(rescale=1.0 / 255, horizontal_flip=True, width_shift_range=4.0 / 32.0, height_shift_range=4.0 / 32.0) test_gen = ImageDataGenerator(rescale=1.0 / 255) # train_gen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True, zca_whitening=True, horizontal_flip=True,) # test_gen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True, zca_whitening=True) y_train = to_categorical(y_train) y_test = to_categorical(y_test) # load network model = ShuffleNet_V2() opt = SGD(0.01, momentum=0.9) #model.compile(Adam(0.001), "categorical_crossentropy", ["accuracy"]) model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=['accuracy']) #model.compile(SGD(0.01, momentum = 0.9), "categorical_crossentropy", ["acc", "top_k_categorical_accuracy"]) model.summary() # set GPU config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config) session.run(tf.global_variables_initializer()) KTF.set_session(session) # set batch_size = 128 scheduler = LearningRateScheduler(lr_scheduler) hist = History() start_time = time.time() #model.fit_generator(train_gen.flow(X_train, y_train, batch_size, shuffle=True), # steps_per_epoch=X_train.shape[0]//batch_size, # validation_data=test_gen.flow(X_test, y_test, batch_size, shuffle=False), # validation_steps=X_test.shape[0]//batch_size, # callbacks=[scheduler, hist], max_queue_size=5, epochs=100) model.fit_generator(train_gen.flow(X_train, y_train, batch_size, shuffle=True), steps_per_epoch=X_train.shape[0] // batch_size, validation_data=test_gen.flow(X_test, y_test, batch_size, shuffle=False), validation_steps=X_test.shape[0] // batch_size, callbacks=[scheduler, hist], max_queue_size=5, epochs=50) elapsed = time.time() - start_time print('training time', elapsed) history = hist.history history["elapsed"] = elapsed with open("shuffle_v2_002_glp.pkl", "wb") as fp: pickle.dump(history, fp)
def cnn_classify(train_data_cwt, uci_har_labels_train, test_data_cwt, uci_har_labels_test, epochs): history = History() x_train = train_data_cwt y_train = list(uci_har_labels_train) x_test = test_data_cwt y_test = list(uci_har_labels_test) num_classes = 6 batch_size = 16 # reshape the data into a 4D tensor - (sample_number, x_img_size, y_img_size, num_channels) # because the MNIST is greyscale, we only have a single channel - RGB colour images would have 3 input_shape = np.shape(train_data_cwt)[1:] # convert the data to the right type x_train = x_train.astype('float32') x_test = x_test.astype('float32') print("x_train shape: {}, x_test shape: {}".format(x_train.shape, x_test.shape)) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices - this is for use in the # categorical_crossentropy loss below y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add( Conv2D(32, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(64, (5, 5), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(1000, activation='relu')) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test), callbacks=[history]) train_score = model.evaluate(x_train, y_train, verbose=0) #print('Train loss: {}, Train accuracy: {}'.format(train_score[0], train_score[1])) test_score = model.evaluate(x_test, y_test, verbose=0) #print('Test loss: {}, Test accuracy: {}'.format(test_score[0], test_score[1])) return train_score, test_score, history
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180) zoom_range=0.1, # Randomly zoom image width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False) # randomly flip images datagen.fit(X_train) model = HistoryManager.LoadToModel( setModel) # 두번째 매개변수로 파일명(.h5) 입력시 이미 학습된 모델 로드 default는 새 학습 model.compile(optimizer=rmsprop, loss='categorical_crossentropy', metrics=['accuracy']) History() # keras history 셋업 history = model.fit(datagen.flow(X_train, Y_train, batch_size=batch_size), epochs=epochs, validation_data=(X_val, Y_val), verbose=2, steps_per_epoch=X_train.shape[0] // batch_size, callbacks=[learning_rate_reduction]) HistoryManager.LoggingHistory(history=history, time=time) HistoryManager.SaveToModel(model, directory="../Mnist/models/", time=time) pred = model.predict(test) # 정답에 맞는 포매팅, 저장함수에 콜백으로 전달 def Submission(prediction):
def test(self, env, nb_episodes=1, action_repetition=1, callbacks=None, visualize=True, nb_max_episode_steps=None, nb_max_start_steps=0, start_step_policy=None, verbose=1): if not self.compiled: raise RuntimeError('Your tried to test your agent but it hasn\'t been compiled yet. Please call `compile()` before `test()`.') if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format(action_repetition)) self.training = False self.step = 0 callbacks = [] if not callbacks else callbacks[:] if verbose >= 1: callbacks += [TestLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_episodes': nb_episodes, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_test_begin() callbacks.on_train_begin() for episode in range(nb_episodes): callbacks.on_episode_begin(episode) episode_reward = 0. episode_step = 0 # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint(nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, r, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step(observation, r, done, info) callbacks.on_action_end(action) if done: warnings.warn('Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.'.format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) break # Run the episode until we're done. done = False while not done: callbacks.on_step_begin(episode_step) oldaction = self.forward(observation) if self.shield is not None: if self.maze: inp = get_input_maze(observation) else: inp = get_input(observation) action_bin = to_bin(oldaction) #sleep(0.01) action = to_int(self.shield.move(inp[0],inp[1],inp[2],inp[3],action_bin[0],action_bin[1],action_bin[2])) else: action = oldaction if self.processor is not None: action = self.processor.process_action(action) reward = 0. accumulated_info = {} for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, d, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, d, info = self.processor.process_step(observation, r, d, info) callbacks.on_action_end(action) reward += r for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value if d: done = True break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: done = True self.backward(reward, terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 self.step += 1 # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # Report end of episode. episode_logs = { 'episode_reward': episode_reward, 'nb_steps': episode_step, } callbacks.on_episode_end(episode, episode_logs) callbacks.on_train_end() self._on_test_end() return history
def on_epoch_end(self, epoch, logs={}): History.on_epoch_end(self, epoch, logs) record_results(self, self.model, self.location) make_plots(self, self.model, self.location, self.name)