def _build_model(self): model = self.load() if model is None: main_input = Input(shape=self.input_dim, name='main_input') block = self._add_conv_block( prev_block=main_input, filters=self.layers_metadata[0]['filters'], kernel_size=self.layers_metadata[0]['kernel_size']) if len(self.layers_metadata) > 1: for metadata in self.layers_metadata[1:]: block = self._add_residual_block( prev_block=block, filters=metadata['filters'], kernel_size=metadata['kernel_size']) value_head = self._add_value_head(prev_block=block) policy_head = self._add_policy_head(prev_block=block) model = Model(inputs=main_input, outputs=[value_head, policy_head]) model.compile(loss={ 'value_head': 'mse', 'policy_head': 'categorical_crossentropy' }, optimizer=SGD(lr=self.learning_rate), loss_weights={ 'value_head': 0.5, 'policy_head': 0.5 }, metrics={ 'value_head': 'mse', 'policy_head': 'acc' }) # invoke following 3 protected methods to make the model can be shared by multiple threads model._make_predict_function() model._make_train_function() model._make_test_function() return model
class OldDroneNet(UnitNet): _in_channel = 18 _out_channel = 6 def __init__(self, loading=False): self._in_channel = OldDroneNet._in_channel self._out_channel = OldDroneNet._out_channel # super(DroneNet,self).__init__(loading) self.session = KTF.get_session() self.graph = tf.get_default_graph() with self.session.as_default(): with self.graph.as_default(): self.inp = Input((WINDOW_SIZE, WINDOW_SIZE, self._in_channel), dtype='float32') # self.conv1=conv_block(self.inp,1,True) self.conv1 = Conv2D(32, (1, 1), activation='relu', padding='same')(self.inp) self.pool1 = MaxPooling2D((2, 2))(self.conv1) self.conv2 = conv_block(self.pool1, 1) self.pool2 = MaxPooling2D((2, 2))(self.conv2) self.conv3 = conv_block(self.pool2, 1) self.pool3 = MaxPooling2D((2, 2))(self.conv3) self.deconv1 = deconv_block(self.pool3, 2) self.up1 = UpSampling2D((2, 2))(self.deconv1) self.deconv2 = deconv_block( Concatenate(axis=3)([self.up1, self.conv3]), 1) self.up2 = UpSampling2D((2, 2))(self.deconv2) self.deconv3 = deconv_block( Concatenate(axis=3)([self.up2, self.conv2]), 1) self.up3 = UpSampling2D((2, 2))(self.deconv3) # self.deconv4=deconv_block(Concatenate(axis=3)([self.up3,self.conv1]),1) self.deconv4 = Conv2DTranspose(64, (3, 3), activation='relu', padding='same')(self.up3) self.out = Conv2DTranspose(DroneNet._out_channel, (3, 3), activation='softmax', padding='same')(self.deconv4) # self.deconv6.set_shape([None,WINDOW_SIZE,WINDOW_SIZE,OUT_CHANNEL]) self.model = Model(inputs=self.inp, outputs=self.out) # print(self.model.summary()) opt = Adam(lr=0.0001) self.model.compile(optimizer=opt, loss='categorical_crossentropy') self.model._make_predict_function() self.model._make_test_function() self.model._make_train_function() if (os.path.isfile('DroneNet.h5') and loading == True): self.model = load_model("DroneNet.h5") # self.model._make_prediction_function() @staticmethod def msg2mask(disGame, msg): ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, OldDroneNet._out_channel]) ans[WINDOW_SIZE // 2, WINDOW_SIZE // 2, 0] = 1 ans[:, :, 5] = (msg[1][1] or msg[1][2]) x = msg[0][0] y = msg[0][1] ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) hei, wid = disGame.regions.shape ans[ax:min(WINDOW_SIZE, hei - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, wid - y + WINDOW_SIZE // 2), 1] = disGame.regions[ max(0, x - WINDOW_SIZE // 2):min(x + WINDOW_SIZE // 2, hei), max(0, y - WINDOW_SIZE // 2):min(y + WINDOW_SIZE // 2, wid)] if (msg[1][3] == 0): for i in msg[2]: ans[i[0][0] - x + WINDOW_SIZE // 2, i[0][1] - y + WINDOW_SIZE // 2, 4] = 1 - i[2] for i in msg[3]: ans[i[0][0] - x + WINDOW_SIZE // 2, i[0][1] - y + WINDOW_SIZE // 2, 4] = 1 - i[2] for i in msg[4]: if (i[0]): ans[i[1][0] - x + WINDOW_SIZE // 2, i[1][1] - y + WINDOW_SIZE // 2, 3] = 1 for i in msg[5]: ans[i[1][0] - x + WINDOW_SIZE // 2, i[1][1] - y + WINDOW_SIZE // 2, 3] = 1 return ans @staticmethod def y2state(ind): ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, OldDroneNet._out_channel]) if (ind[2] in [0, 5]): ans[WINDOW_SIZE // 2, WINDOW_SIZE // 2, ind[2]] = 1 else: ans[shrinkScr(ind[0]), shrinkScr(ind[1]), ind[2]] = 1 return ans @staticmethod def msg2state(disGame, msg): ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, OldDroneNet._in_channel]) x, y = msg[0] ans[:, :, 11] = msg[1][0] ans[:, :, 16] = msg[1][1] ans[:, :, 17] = msg[1][2] for u in msg[2]: nx = u[0][0] - x + WINDOW_SIZE // 2 ny = u[0][1] - y + WINDOW_SIZE // 2 if (u[2]): ans[nx, ny, 5] = 1 elif (u[3]): ans[nx, ny, 6] = 1 else: ans[nx, ny, 4] = 1 ans[nx, ny, 13] = u[1] ans[nx, ny, 14] = u[4] ans[nx, ny, 15] = u[5] for u in msg[3]: nx = u[0][0] - x + WINDOW_SIZE // 2 ny = u[0][1] - y + WINDOW_SIZE // 2 if (u[2]): ans[nx, ny, 5] = 1 elif (u[3]): ans[nx, ny, 6] = 1 else: ans[nx, ny, 4] = 1 ans[nx, ny, 12] = u[1] for u in msg[4]: nx = u[1][0] - x + WINDOW_SIZE // 2 ny = u[1][1] - y + WINDOW_SIZE // 2 if (u[0]): ans[nx, ny, 7] = 1 else: ans[nx, ny, 8] = 1 for u in msg[5]: nx = u[0] - x + WINDOW_SIZE // 2 ny = u[1] - y + WINDOW_SIZE // 2 # print(u,x,y,nx,ny) ans[nx, ny, 9] = 1 ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) X = disGame.hground.shape[0] Y = disGame.hground.shape[1] ''' print(ax,min(WINDOW_SIZE,X-x+WINDOW_SIZE//2), ay,min(WINDOW_SIZE,Y-y+WINDOW_SIZE//2), max(0,x-WINDOW_SIZE//2),min(x+WINDOW_SIZE//2,X), max(0,y-WINDOW_SIZE//2),min(y+WINDOW_SIZE//2,Y)) ''' ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 10] = disGame.hground[ max(0, x - WINDOW_SIZE // 2):min(x + WINDOW_SIZE // 2, X), max(0, y - WINDOW_SIZE // 2):min(y + WINDOW_SIZE // 2, Y)] ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 0] = disGame.regions[ max(0, x - WINDOW_SIZE // 2):min(x + WINDOW_SIZE // 2, X), max(0, y - WINDOW_SIZE // 2):min(y + WINDOW_SIZE // 2, Y)] return ans
class Wide_ResNet(object): ''' Modified from: https://gist.github.com/kashif/0ba0270279a0f38280423754cea2ee1e ''' def __init__(self, config): self.verbose = config['verbose'] self.size = config['size'] self.rank = config['rank'] self.name = 'Wide_ResNet' # data from data.cifar10 import Cifar10_data self.data = Cifar10_data(verbose=False) self.build_model() # iter related self.current_t = 0 self.last_one_t = False self.current_v = 0 self.last_one_v = False self.n_subb = 1 self.n_epochs = nb_epoch self.epoch = 0 def build_model(self): img_input = Input(shape=(img_channels, img_rows, img_cols)) # one conv at the beginning (spatial size: 32x32) x = ZeroPadding2D((1, 1))(img_input) x = Convolution2D(16, nb_row=3, nb_col=3)(x) # Stage 1 (spatial size: 32x32) x = bottleneck(x, n, 16, 16 * k, dropout=0.3, subsample=(1, 1)) # Stage 2 (spatial size: 16x16) x = bottleneck(x, n, 16 * k, 32 * k, dropout=0.3, subsample=(2, 2)) # Stage 3 (spatial size: 8x8) x = bottleneck(x, n, 32 * k, 64 * k, dropout=0.3, subsample=(2, 2)) x = BatchNormalization(mode=0, axis=1)(x) x = Activation('relu')(x) x = AveragePooling2D((8, 8), strides=(1, 1))(x) x = Flatten()(x) preds = Dense(nb_classes, activation='softmax')(x) self.model = Model(input=img_input, output=preds) self.keras_get_params() def compile_iter_fns(self, *args, **kwargs): try: sync_type = kwargs['sync_type'] except: raise RuntimeError( 'Keras wresnet needs sync_type keyword argument') if sync_type != 'avg': raise RuntimeError( 'currently wresnet only support compiling with sync_type avg, add BSP_SYNC_TYPE=avg in your session cfg' ) import time start = time.time() self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) self.shared_lr = self.model.optimizer.lr self.base_lr = self.shared_lr.get_value() if self.verbose: print('Compiling......') self.model._make_train_function() self.model._make_test_function() if self.verbose: print('Compile time: %.3f s' % (time.time() - start)) self.data.batch_data(self.model, batch_size) self.data.extend_data(rank=self.rank, size=self.size) self.data.shuffle_data(mode='train', common_seed=1234) self.data.shuffle_data(mode='val') self.data.shard_data(mode='train', rank=self.rank, size=self.size) # to update data.n_batch_train self.data.shard_data(mode='val', rank=self.rank, size=self.size) # to update data.n_batch_val def train_iter(self, count, recorder): if self.current_t == 0: self.data.shuffle_data(mode='train', common_seed=self.epoch) self.data.shard_data(mode='train', rank=self.rank, size=self.size) recorder.start() cost, acc = self.model.train_function( self.data.train_batches_shard[self.current_t]) recorder.train_error(count, cost, 1.0 - acc) recorder.end('calc') if self.current_t == self.data.n_batch_train - 1: self.last_one_t = True else: self.last_one_t = False if self.last_one_t == False: self.current_t += 1 else: self.current_t = 0 def val_iter(self, count, recorder): if self.current_v == 0: self.data.shuffle_data(mode='val') self.data.shard_data(mode='val', rank=self.rank, size=self.size) cost, acc = self.model.test_function( self.data.val_batches_shard[self.current_v]) recorder.val_error(count, cost, 1.0 - acc, 0) if self.current_v == self.data.n_batch_val - 1: self.last_one_v = True else: self.last_one_v = False if self.last_one_v == False: self.current_v += 1 else: self.current_v = 0 def reset_iter(self, mode): '''used at the begininig of another mode''' if mode == 'train': self.current_t = 0 self.subb_t = 0 self.last_one_t = False else: self.current_v = 0 self.subb_v = 0 self.last_one_v = False def adjust_hyperp(self, epoch): 'to be called once per epoch' if lr_policy == 'step': if epoch in lr_step: tuned_base_lr = self.shared_lr.get_value() / 10. self.shared_lr.set_value(np.float32(tuned_base_lr)) def scale_lr(self, size): self.shared_lr.set_value(np.array(self.base_lr * size, dtype='float32')) def cleanup(self): pass def keras_get_params(self): self.params = [] for l in self.model.layers: self.params.extend(l.trainable_weights)
class PseudoLabel: """ Pseudo-label Class """ def __init__(self, image_width=256, image_height=256, train_data_directory="../data/train", validation_data_directory="../data/validation", test_data_directory="../data/test", no_label_data_directory="../data/no_label", batch_size=8, pseudo_label_batch_size=16, epochs=1, class_names=[], architecture="VGG16", image_channels=3, learnin_rate=0.001, save_heights=False, transfer_learning={ 'use_transfer_learning': False, 'fine_tuning': None }, optimizer='SGD', metrics_list=['acc'], h5_filename=None, class_labels=None, alpha=0.5, print_pseudo_generate=False): """ Pseudo-label class construtor """ # Atributes declarations self.image_width = image_width self.image_height = image_height self.train_data_directory = train_data_directory self.validation_data_directory = validation_data_directory self.test_data_directory = test_data_directory self.no_label_data_directory = no_label_data_directory self.batch_size = batch_size self.pseudo_label_batch_size = pseudo_label_batch_size self.epochs = epochs self.class_names = class_names self.architecture = architecture self.image_channels = image_channels self.learning_rate = learnin_rate self.use_transfer_learning = transfer_learning.get( 'use_transfer_learning') self.fine_tuning_rate = transfer_learning.get('fine_tuning') self.optimizer = optimizer self.metrics_list = metrics_list self.model = None self.train_generator = None self.validation_generator = None self.h5_filename = h5_filename self.class_labels = class_labels self.alpha = alpha self.print_pseudo_generate = print_pseudo_generate # Make your model and dataset self.make_data_generators() self.make_model(architecture=self.architecture, use_transfer_learning=self.use_transfer_learning, fine_tuning_rate=self.fine_tuning_rate, optimizer=self.optimizer, metrics_list=self.metrics_list) self.generate_h5_filename() def make_model(self, architecture=None, use_transfer_learning=False, fine_tuning_rate=None, optimizer='SGD', metrics_list=['accuracy']): """ Create your CNN keras model Arguments: architecture (str): architecture of model """ # Validations for metric in metrics_list: if metric not in LIST_OF_ACCEPTABLES_METRICS: raise ValueError("The specified metric \'" + metric + "\' is not supported") if optimizer not in LIST_OF_ACCEPTABLES_OPTIMIZERS.keys(): raise ValueError("The specified optimizer \'" + optimizer + "\' is not supported!") if architecture not in LIST_OF_ACCEPTABLES_ARCHITECTURES.keys(): raise ValueError("The specified architecture \'" + architecture + "\' is not supported!") else: if use_transfer_learning and not 0 <= fine_tuning_rate <= 100: raise ValueError( "The fine tuning rate must be beetween 0 and 100!") if use_transfer_learning and fine_tuning_rate == None: raise ValueError( "You need to specify a fine tuning rate if you're using transfer learning!" ) # With transfer learning if use_transfer_learning: self.model = LIST_OF_ACCEPTABLES_ARCHITECTURES.get(architecture)( weights="imagenet", include_top=False, input_shape=(self.image_height, self.image_width, self.image_channels)) last_layers = len(self.model.layers) - \ int(len(self.model.layers) * (fine_tuning_rate / 100.)) for layer in self.model.layers[:last_layers]: layer.trainable = False # Without transfer learning else: self.model = LIST_OF_ACCEPTABLES_ARCHITECTURES.get(architecture)( weights=None, include_top=False, input_shape=(self.image_height, self.image_width, self.image_channels)) for layer in self.model.layers: layer.trainable = True # Adding the custom Layers new_custom_layers = self.model.output new_custom_layers = Flatten()(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) new_custom_layers = Dropout(0.5)(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) try: predictions = Dense(self.train_generator.num_classes, activation="softmax")(new_custom_layers) except AttributeError: predictions = Dense(self.train_generator.num_class, activation="softmax")(new_custom_layers) # Create the final model self.model = Model(inputs=self.model.input, outputs=predictions) # Compile model self.model.compile( loss=self.pseudo_label_loss_function, optimizer=LIST_OF_ACCEPTABLES_OPTIMIZERS.get(optimizer)( lr=self.learning_rate), metrics=metrics_list) def pseudo_label_loss_function(self, y_true, y_pred): loss_true_label = self.cross_entropy(y_true[:self.batch_size], y_pred[:self.batch_size]) loss_pseudo_label = (self.cross_entropy(y_true[self.batch_size:], y_pred[self.batch_size:])) return (loss_true_label / self.batch_size) + ( self.alpha * (loss_pseudo_label / self.pseudo_label_batch_size)) def cross_entropy(self, targets, predictions, epsilon=1e-12): predictions = K.clip(predictions, epsilon, 1. - epsilon) N = predictions.shape[0] log1 = K.log(predictions + 1e-9) sum1 = K.sum(targets * log1) if (predictions.shape[0].value is not None): return -K.sum(sum1) / N else: return -K.sum(sum1) def make_data_generators(self, use_data_augmentation=False): """ Function that initiate the train, validation and test generators with data augumentation """ self.train_generator = ImageDataGenerator().flow_from_directory( self.train_data_directory, target_size=(self.image_height, self.image_width), color_mode='rgb', classes=self.class_labels, batch_size=self.batch_size, shuffle=True, class_mode="categorical") self.test_generator = ImageDataGenerator().flow_from_directory( self.test_data_directory, target_size=(self.image_height, self.image_width), color_mode='rgb', batch_size=1, shuffle=False, class_mode="categorical") self.validation_generator = ImageDataGenerator().flow_from_directory( self.validation_data_directory, target_size=(self.image_height, self.image_width), color_mode='rgb', batch_size=self.batch_size, shuffle=True, class_mode="categorical") self.no_label_generator = ImageDataGenerator().flow_from_directory( self.no_label_data_directory, target_size=(self.image_height, self.image_width), color_mode='rgb', batch_size=self.pseudo_label_batch_size, shuffle=False, class_mode="categorical") try: self.no_label_generator.num_classes = self.validation_generator.num_classes except AttributeError: self.no_label_generator.num_class = self.validation_generator.num_class def generate_h5_filename(self): """ Generate the .h5 filename. The .h5 file is the file that contains your trained model """ if self.fine_tuning_rate == 100: self.h5_filename = self.architecture + \ '_transfer_learning' elif self.fine_tuning_rate == None: self.h5_filename = self.architecture + \ '_without_transfer_learning' else: self.h5_filename = self.architecture + \ '_fine_tunning_' + str(self.fine_tuning_rate) ################################################################################ # Semi-supervised - Pseudo label approach ################################################################################ def fit_with_pseudo_label(self, steps_per_epoch, validation_steps=None, use_checkpoints=True, class_labels=None, verbose=1, use_multiprocessing=False, shuffle=False, workers=1, max_queue_size=10): # Default value if validation steps is none if (validation_steps == None): validation_steps = self.validation_generator.samples // self.batch_size wait_time = 0.01 # in seconds self.model._make_train_function() # Create a checkpoint callback checkpoint = ModelCheckpoint("../models_checkpoints/" + str(self.h5_filename) + ".h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='auto', period=1) # Generate callbacks callback_list = [] if use_checkpoints: callback_list.append(checkpoint) # Init train counters epoch = 0 validation_data = self.validation_generator do_validation = bool(validation_data) self.model._make_train_function() if do_validation: self.model._make_test_function() val_gen = (hasattr(validation_data, 'next') or hasattr(validation_data, '__next__') or isinstance(validation_data, Sequence)) if (val_gen and not isinstance(validation_data, Sequence) and not validation_steps): raise ValueError('`validation_steps=None` is only valid for a' ' generator based on the `keras.utils.Sequence`' ' class. Please specify `validation_steps` or use' ' the `keras.utils.Sequence` class.') # Prepare display labels. out_labels = self.model.metrics_names callback_metrics = out_labels + ['val_' + n for n in out_labels] # Prepare train callbacks self.model.history = cbks.History() callbacks = [cbks.BaseLogger()] + (callback_list or []) + \ [self.model.history] if verbose: callbacks += [cbks.ProgbarLogger(count_mode='steps')] callbacks = cbks.CallbackList(callbacks) # it's possible to callback a different model than self: if hasattr(self.model, 'callback_model') and self.model.callback_model: callback_model = self.model.callback_model else: callback_model = self.model callbacks.set_model(callback_model) is_sequence = isinstance(self.train_generator, Sequence) if not is_sequence and use_multiprocessing and workers > 1: warnings.warn( UserWarning('Using a generator with `use_multiprocessing=True`' ' and multiple workers may duplicate your data.' ' Please consider using the`keras.utils.Sequence' ' class.')) if is_sequence: steps_per_epoch = len(self.train_generator) enqueuer = None val_enqueuer = None callbacks.set_params({ 'epochs': self.epochs, 'steps': steps_per_epoch, 'verbose': verbose, 'do_validation': do_validation, 'metrics': callback_metrics, }) callbacks.on_train_begin() try: if do_validation and not val_gen: # Prepare data for validation if len(validation_data) == 2: val_x, val_y = validation_data val_sample_weight = None elif len(validation_data) == 3: val_x, val_y, val_sample_weight = validation_data else: raise ValueError('`validation_data` should be a tuple ' '`(val_x, val_y, val_sample_weight)` ' 'or `(val_x, val_y)`. Found: ' + str(validation_data)) val_x, val_y, val_sample_weights = self.model._standardize_user_data( val_x, val_y, val_sample_weight) val_data = val_x + val_y + val_sample_weights if self.model.uses_learning_phase and not isinstance( K.learning_phase(), int): val_data += [0.] for cbk in callbacks: cbk.validation_data = val_data if is_sequence: enqueuer = OrderedEnqueuer( self.train_generator, use_multiprocessing=use_multiprocessing, shuffle=shuffle) else: enqueuer = GeneratorEnqueuer( self.train_generator, use_multiprocessing=use_multiprocessing, wait_time=wait_time) enqueuer.start(workers=workers, max_queue_size=max_queue_size) output_generator = enqueuer.get() # Train the model # Construct epoch logs. epoch_logs = {} # Epochs while epoch < self.epochs: callbacks.on_epoch_begin(epoch) steps_done = 0 batch_index = 0 # Steps per epoch while steps_done < steps_per_epoch: generator_output = next(output_generator) if len(generator_output) == 2: x, y = generator_output sample_weight = None elif len(generator_output) == 3: x, y, sample_weight = generator_output else: raise ValueError('Output of generator should be ' 'a tuple `(x, y, sample_weight)` ' 'or `(x, y)`. Found: ' + str(generator_output)) #========================== # Mini-batch #========================== if (self.print_pseudo_generate): print '' print 'Generating pseudo-labels...' verbose = 1 else: verbose = 0 if self.no_label_generator.samples > 0: no_label_output = self.model.predict_generator( self.no_label_generator, self.no_label_generator.samples, verbose=verbose) # One-hot encoded self.no_label_generator.classes = np.argmax( no_label_output, axis=1) # Concat Pseudo labels with true labels x_pseudo, y_pseudo = next(self.no_label_generator) x, y = np.concatenate((x, x_pseudo), axis=0), np.concatenate( (y, y_pseudo), axis=0) # build batch logs batch_logs = {} if isinstance(x, list): batch_size = x[0].shape[0] elif isinstance(x, dict): batch_size = list(x.values())[0].shape[0] else: batch_size = x.shape[0] batch_logs['batch'] = batch_index batch_logs['size'] = batch_size callbacks.on_batch_begin(batch_index, batch_logs) # Runs a single gradient update on a single batch of data scalar_training_loss = self.model.train_on_batch(x=x, y=y) if not isinstance(scalar_training_loss, list): scalar_training_loss = [scalar_training_loss] for l, o in zip(out_labels, scalar_training_loss): batch_logs[l] = o callbacks.on_batch_end(batch_index, batch_logs) #========================== # end Mini-batch #========================== batch_index += 1 steps_done += 1 if steps_done >= steps_per_epoch and do_validation: if val_gen: val_outs = self.model.evaluate_generator( validation_data, validation_steps, workers=workers, use_multiprocessing=use_multiprocessing, max_queue_size=max_queue_size) else: # No need for try/except because # data has already been validated. val_outs = self.model.evaluate( val_x, val_y, batch_size=batch_size, sample_weight=val_sample_weights, verbose=0) if not isinstance(val_outs, list): val_outs = [val_outs] # Same labels assumed. for l, o in zip(out_labels, val_outs): epoch_logs['val_' + l] = o # Epoch finished. callbacks.on_epoch_end(epoch, epoch_logs) epoch += 1 finally: try: if enqueuer is not None: enqueuer.stop() finally: if val_enqueuer is not None: val_enqueuer.stop() callbacks.on_train_end() return self.model.history
class DragoonNet(UnitNet): _in_channel = 10 _out_channel = 6 def __init__(self, loading=False): #global common_graph, common_session self._in_channel = DragoonNet._in_channel self._out_channel = DragoonNet._out_channel #self.session=common_session #self.graph=common_graph self.graph = tf.Graph() self.session = tf.Session(graph=self.graph) with self.session.as_default(): with self.graph.as_default(): self.inp = Input((WINDOW_SIZE, WINDOW_SIZE, self._in_channel), dtype='float32') self.conv1 = Conv2D(32, (1, 1), activation='linear', padding='same')(self.inp) self.pool1 = MaxPooling2D((2, 2))(self.conv1) self.conv2 = conv_block(self.pool1, 1) self.pool2 = MaxPooling2D((2, 2))(self.conv2) self.conv3 = conv_block(self.pool2, 1) self.pool3 = MaxPooling2D((2, 2))(self.conv3) self.deconv1 = deconv_block(self.pool3, 2) self.up1 = UpSampling2D((2, 2))(self.deconv1) self.deconv2 = deconv_block( Concatenate(axis=3)([self.up1, self.conv3]), 1) self.up2 = UpSampling2D((2, 2))(self.deconv2) self.deconv3 = deconv_block( Concatenate(axis=3)([self.up2, self.conv2]), 1) self.up3 = UpSampling2D((2, 2))(self.deconv3) self.deconv4 = Conv2DTranspose(64, (3, 3), activation='relu', padding='same')(self.up3) self.out = Conv2DTranspose(self._out_channel, (3, 3), activation='linear', padding='same')(self.deconv4) self.model = Model(inputs=self.inp, outputs=self.out) self.model.compile(optimizer='adam', loss='MSE') self.model._make_predict_function() self.model._make_test_function() self.model._make_train_function() if (loading and os.path.isfile('DragoonNet.h5')): self.model = load_model('DragoonNet.h5') def save(self): with self.session.as_default(): with self.graph.as_default(): self.model.save('DragoonNet.h5') @staticmethod def msg2state(disGame, msg): x, y = msg.myInfo.coord X, Y = disGame.regions.shape ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, DragoonNet._in_channel]) ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 0] = disGame.regions[ max(0, x - WINDOW_SIZE // 2):min(x + WINDOW_SIZE // 2, X), max(0, y - WINDOW_SIZE // 2):min(y + WINDOW_SIZE // 2, Y)] for u in msg.enemies: #enemy nx = u.coord[0] - x + WINDOW_SIZE // 2 ny = u.coord[1] - y + WINDOW_SIZE // 2 ans[nx, ny, 2] = 1 ans[nx, ny, 5] = u.HP + u.shield ans[nx, ny, 6] = u.damageTo ans[nx, ny, 7] = u.damagaFrom for u in msg.allies: #ally nx = u.coord[0] - x + WINDOW_SIZE // 2 ny = u.coord[1] - y + WINDOW_SIZE // 2 ans[nx, ny, 1] = 1 ans[nx, ny, 4] = u.HP + u.shield ans[:, :, 3] = msg.myInfo.HP + msg.myInfo.shield for i in range(WINDOW_SIZE * 32 // X): for j in range(WINDOW_SIZE * 32 // X): #print(ans[i::WINDOW_SIZE*32//X,j::WINDOW_SIZE*32//X,8].shape,msg[6].shape) ans[i::WINDOW_SIZE * 32 // X, j::WINDOW_SIZE * 32 // X, 8] = msg.explored ans[x * WINDOW_SIZE // X, y * WINDOW_SIZE // Y, 9] = 1 return ans @staticmethod def msg2mask(disGame, msg): ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, DragoonNet._out_channel]) x, y = msg.myInfo.coord X, Y = disGame.regions.shape ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) ans[WINDOW_SIZE // 2, WINDOW_SIZE // 2, 0] = 1 ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 1] = disGame.regions[ max(0, x - WINDOW_SIZE // 2):min(x + WINDOW_SIZE // 2, X), max(0, y - WINDOW_SIZE // 2):min(y + WINDOW_SIZE // 2, Y)] for u in msg.enemies: nx = u.coord[0] - x + WINDOW_SIZE // 2 ny = u.coord[1] - y + WINDOW_SIZE // 2 ans[nx, ny, 2] = 1 top, bot, left, right = u.bounds #ans[shrinkScr(top - x + WINDOW_SIZE // 2):shrinkScr(bot - x + WINDOW_SIZE // 2), # shrinkScr(left - y + WINDOW_SIZE // 2):shrinkScr(right - x + WINDOW_SIZE // 2),1] = 0 for u in msg.allies: top, bot, left, right = u.bounds #ans[shrinkScr(top - x + WINDOW_SIZE // 2):shrinkScr(bot - x + WINDOW_SIZE // 2), # shrinkScr(left - y + WINDOW_SIZE // 2):shrinkScr(right - x + WINDOW_SIZE // 2),1] = 0 return ans
class VultureNet(UnitNet): _in_channel = 8 _out_channel = 3 def __init__(self, loading=False, output_type='linear'): self._in_channel = VultureNet._in_channel self._out_channel = VultureNet._out_channel self.session = KTF.get_session() self.graph = tf.get_default_graph() with self.session.as_default(): with self.graph.as_default(): self.inp = Input((WINDOW_SIZE, WINDOW_SIZE, self._in_channel), dtype='float32') self.conv1 = LeakyReLU()(Conv2D(128, (3, 3), padding='same')(self.inp)) self.conv1 = conv_block(self.conv1, 2) self.pool1 = MaxPooling2D((2, 2))(self.conv1) self.conv2 = conv_block(self.pool1, 2) self.pool2 = MaxPooling2D((2, 2))(self.conv2) self.conv3 = conv_block(self.pool2, 2) self.pool3 = MaxPooling2D((2, 2))(self.conv3) self.deconv1 = deconv_block(self.pool3, 1) self.up1 = UpSampling2D((2, 2))(self.deconv1) self.deconv2 = deconv_block( Concatenate(axis=3)([self.up1, self.conv3]), 1) self.up2 = UpSampling2D((2, 2))(self.deconv2) self.deconv3 = deconv_block( Concatenate(axis=3)([self.up2, self.conv2]), 1) self.up3 = UpSampling2D((2, 2))(self.deconv3) self.deconv4 = LeakyReLU()(Conv2DTranspose( 128, (3, 3), padding='same')(Concatenate(axis=3)([self.up3, self.conv1]))) if (output_type == 'softmax'): self.out = Conv2DTranspose(self._out_channel, (3, 3), padding='same')(self.deconv4) self.out = Flatten()(self.out) self.out = Activation('softmax')(self.out) self.out = Reshape( [-1, WINDOW_SIZE, WINDOW_SIZE, self._out_channel])(self.out) else: self.out = Conv2DTranspose(self._out_channel, (3, 3), padding='same')(self.deconv4) #optz=SGD(lr=0.001,momentum=0.9) self.model = Model(inputs=self.inp, outputs=self.out) self.optz = Adam() self.model.compile(optimizer=self.optz, loss='MSE') self.model._make_predict_function() self.model._make_test_function() self.model._make_train_function() if (os.path.isfile('VultureNet.h5') and loading): self.model = load_model("VultureNet.h5") def save(self): with self.session.as_default(): with self.graph.as_default(): self.model.save('VultureNet.h5') @staticmethod def msg2state(disGame, msg): #[enemyBound, myBound, region, myCooldown, myRange, enemyCooldown, myHp, enemyCenter] x, y = msg.myInfo.coord X, Y = disGame.regions.shape ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, VultureNet._in_channel]) for u in msg.enemies: if (abs(u.coord[0] - x) < WINDOW_SIZE // 2 and abs(u.coord[1] - y) < WINDOW_SIZE // 2): ans[shrinkScr(u.bounds[0] - x + WINDOW_SIZE // 2):shrinkScr(u.bounds[1] - x + WINDOW_SIZE // 2), shrinkScr(u.bounds[2] - y + WINDOW_SIZE // 2):shrinkScr(u.bounds[3] - y + WINDOW_SIZE // 2), 1] = 1 ans[u.coord[0] - x + WINDOW_SIZE // 2, u.coord[1] - y + WINDOW_SIZE // 2, 7] = 1 ans[u.coord[0] - x + WINDOW_SIZE // 2, u.coord[1] - y + WINDOW_SIZE // 2, 5] = u.canFireGround ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) ans[(msg.myInfo.bounds[0] - x) + WINDOW_SIZE // 2:msg.myInfo.bounds[1] - x + WINDOW_SIZE // 2, (msg.myInfo.bounds[2] - y) + WINDOW_SIZE // 2:msg.myInfo.bounds[3] - y + WINDOW_SIZE // 2, 2] = 1 ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 0] = 1 ans[:, :, 3] = msg.myInfo.canFireGround rng = msg.myInfo.rangeGround[1] for i in range(-rng, rng + 1): for j in range(-int(numpy.sqrt(rng * rng - i * i)), 1 + int(numpy.sqrt(rng * rng - i * i))): ans[WINDOW_SIZE // 2 + i, WINDOW_SIZE // 2 + j, 4] = 1 ans[:, :, 6] = msg.myInfo.HP / 16.0 fstate = open('state.txt', 'wb') pickle.dump(ans, fstate) fstate.close() return ans @staticmethod def msg2mask(disGame, msg): x, y = msg.myInfo.coord X, Y = disGame.regions.shape ans = numpy.zeros([WINDOW_SIZE, WINDOW_SIZE, VultureNet._out_channel]) ax = max(0, WINDOW_SIZE // 2 - x) ay = max(0, WINDOW_SIZE // 2 - y) #ans[WINDOW_SIZE//2,WINDOW_SIZE//2,0]=1 ans[ax:min(WINDOW_SIZE, X - x + WINDOW_SIZE // 2), ay:min(WINDOW_SIZE, Y - y + WINDOW_SIZE // 2), 1] = 1 myPos = msg.myInfo.coord if (msg.myInfo.canFireGround == 0): for u in msg.enemies: if (abs(u.coord[0] - myPos[0]) < WINDOW_SIZE // 2 and abs(u.coord[1] - myPos[1]) < WINDOW_SIZE // 2): ans[u.coord[0] - myPos[0] + WINDOW_SIZE // 2, u.coord[1] - myPos[1] + WINDOW_SIZE // 2, 2] = 1 return ans
## OPTIMIZER dis_op = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=1e-08) cls_op = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08) gen_op = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999, epsilon=1e-08) #gen_op = RMSprop(lr=0.001, rho=0.9, epsilon=1e-06) ## MODEL (gen, (dis, cls)) = get_models(dataset,smallmodel) dis.trainable = False x = Input(shape=(gen_dim,)) h = gen(x) y = dis(h) disgen = Model(input=x, output=y) disgen.compile(loss='binary_crossentropy', optimizer=gen_op, metrics=['accuracy']) disgen._make_train_function() disgen._make_test_function() disgen._make_predict_function() #noise_batch = np.random.uniform(-1, 1, (batch_size, gen_dim)).astype('float32') #labels2 = np.ones((batch_size, 1)).astype('float32') #g_loss = disgen.train_on_batch(noise_batch, labels2) dis.trainable = True dis.compile(loss='binary_crossentropy', optimizer=dis_op, metrics=['accuracy']) if classify: cls.trainable = True cls.compile(loss='categorical_crossentropy', optimizer=cls_op, metrics=['accuracy']) gen.compile(loss='mean_squared_error', optimizer=gen_op)
class myHandler(BaseHTTPRequestHandler): """ Custom functions to process incoming and outgoing signals """ def build_model(self, X_train, y_train): inputs = Input(shape=(128, )) inner = Dense(50, activation='tanh')(inputs) inner = Dropout(0.7)(inner) pred = Dense(1, activation='sigmoid')(inner) self.model = Model(inputs=inputs, outputs=pred) self.model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) self.model._make_predict_function() self.model._make_test_function() self.model._make_train_function() self.model.fit(X_train, y_train, epochs=100) def train_and_test(self, answers): global encode_arr global encode_arr_test answers = answers.split(',') answers = np.array(answers) X = encode_arr y = answers X_train = X[:90, :] y_train = y[:90, ].reshape((90, 1)) X_test = X[90:, :] y_test = y[90:, ].reshape((10, 1)) with tf.Session(graph=tf.Graph()) as sess: K.set_session(sess) self.build_model(X_train, y_train) preds1 = self.model.evaluate(X_test, y_test) print("Loss = " + str(preds1[0])) print("Test Accuracy = " + str(preds1[1])) #predict test data preds_test1 = self.model.predict(encode_arr_test) # divide them to positive results and negative results results_pos = {} results_neg = {} ccnter = 0 for i in preds_test1: if i[0] < 0.35: results_neg[ccnter] = str((1 - i[0]) * 100) + '%' else: results_pos[ccnter] = str(i[0] * 100) + '%' ccnter += 1 # sorting the pos and neg results in descending order oo_pos = OrderedDict( sorted(results_pos.items(), key=lambda x: x[1], reverse=True)) oo_neg = OrderedDict( sorted(results_neg.items(), key=lambda x: x[1], reverse=True)) oo_total_result = {'positive': oo_pos, 'negative': oo_neg} return json.dumps(oo_total_result) """ End of Custom """ #Handler for the GET requests def do_GET(self): print('do GET') #pprint (vars(self)) self.send_response(200) print(self.path) if self.path == '/favicon.ico': return # Send the html message if not os.path.isdir(self.path[1:]): with open(self.path[1:], 'rb') as reader: content = reader.read() else: con = os.listdir(self.path[1:]) len_con = len(con) if con[0] == '.DS_Store': len_con -= 1 content = str(len_con).encode() self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() self.wfile.write(content) return #Handler for the POST requests def do_POST(self): pprint(vars(self)) length = int(self.headers['Content-Length']) form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'}) answers = form['answers'].value print(answers) output = self.train_and_test(answers) # Send back response after the data is processed self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Content-type', 'text/html') self.end_headers() # Send the html message self.wfile.write(output.encode()) return def do_OPTIONS(self): self.send_response(200, "ok") self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS') self.send_header("Access-Control-Allow-Headers", "X-Requested-With") self.send_header("Access-Control-Allow-Headers", "Content-Type") self.end_headers() return
class DroneNet(UnitNet): _in_channel=5 _out_channel=2 def __init__(self,loading=False, output_type='linear'): self._in_channel=DroneNet._in_channel self._out_channel=DroneNet._out_channel self.session = KTF.get_session() self.graph = tf.get_default_graph() with self.session.as_default(): with self.graph.as_default(): self.inp=Input((WINDOW_SIZE,WINDOW_SIZE,self._in_channel),dtype='float32') self.conv1 = LeakyReLU()(Conv2D(128, (3, 3), padding='same')(self.inp)) self.conv1= conv_block(self.conv1,2) self.pool1 = MaxPooling2D((2, 2))(self.conv1) self.conv2 = conv_block(self.pool1, 2) self.pool2 = MaxPooling2D((2, 2))(self.conv2) self.conv3=conv_block(self.pool2, 2) self.pool3=MaxPooling2D((2,2))(self.conv3) self.deconv1 = deconv_block(self.pool3, 1) self.up1 = UpSampling2D((2, 2))(self.deconv1) self.deconv2 = deconv_block(Concatenate(axis=3)([self.up1, self.conv3]), 1) self.up2=UpSampling2D((2,2))(self.deconv2) self.deconv3 = deconv_block(Concatenate(axis=3)([self.up2, self.conv2]), 1) self.up3 = UpSampling2D((2, 2))(self.deconv3) self.deconv4 = LeakyReLU()(Conv2DTranspose(128, (3, 3), padding='same')(Concatenate(axis=3)([self.up3, self.conv1]))) if(output_type=='softmax'): self.out=Conv2DTranspose(DroneNet._out_channel, (3, 3), padding='same')( self.deconv4) self.out=Flatten()(self.out) self.out=Activation('softmax')(self.out) self.out=Reshape([-1,WINDOW_SIZE,WINDOW_SIZE,self._out_channel])(self.out) else: self.out = Conv2DTranspose(DroneNet._out_channel, (3, 3), padding='same')( self.deconv4) #optz=SGD(lr=0.001,momentum=0.9) self.model = Model(inputs=self.inp, outputs=self.out) self.optz=Adam() self.model.compile(optimizer=self.optz, loss='MSE') self.model._make_predict_function() self.model._make_test_function() self.model._make_train_function() if (os.path.isfile('DroneNet.h5') and loading): self.model = load_model("DroneNet.h5") self.mse=keras.losses.mean_squared_error def save(self): with self.session.as_default(): with self.graph.as_default(): self.model.save('DroneNet.h5') def gradient(self, target, X): with self.session.as_default(): with self.graph.as_default(): return tf.gradients(target-self.out, self.model.trainable_weights) #return KTF.gradients(self.mse(target, self.model.predict(X.reshape([-1, WINDOW_SIZE,WINDOW_SIZE,self._in_channel]))), self.model.trainable_weights) @staticmethod def msg2state(disGame, msg): ans=numpy.zeros([WINDOW_SIZE,WINDOW_SIZE,DroneNet._in_channel]) x, y = msg.myInfo.coord X, Y = disGame.regions.shape #print(disGame.name, disGame.regions.shape,x,y) ax=max(0,WINDOW_SIZE//2-x) ay=max(0,WINDOW_SIZE//2-y) ans[ax:min(WINDOW_SIZE,X-x+WINDOW_SIZE//2), ay:min(WINDOW_SIZE,Y-y+WINDOW_SIZE//2),0]=disGame.regions[max(0,x-WINDOW_SIZE//2):min(x+WINDOW_SIZE//2,X), max(0,y-WINDOW_SIZE//2):min(y+WINDOW_SIZE//2,Y)] ans[WINDOW_SIZE//2,WINDOW_SIZE//2,1]=1 for u in msg.allies: if(u.coord[0]!=x or u.coord[1]!=y): if(u.type!='Zerg_Overlord'): if(u.type=='Zerg_Drone'): ans[shrinkScr(u.bounds[0] - x + WINDOW_SIZE // 2):shrinkScr(u.bounds[1] - x + WINDOW_SIZE // 2), shrinkScr(u.bounds[2] - y + WINDOW_SIZE // 2): shrinkScr(u.bounds[3] - y + WINDOW_SIZE // 2), 2]=1 else: ans[shrinkScr(u.bounds[0] - x + WINDOW_SIZE // 2):shrinkScr(u.bounds[1] - x + WINDOW_SIZE // 2), shrinkScr(u.bounds[2] - y + WINDOW_SIZE // 2): shrinkScr(u.bounds[3] - y + WINDOW_SIZE // 2), 3]=1 for u in msg.resources: #print(u.type) if(u.type=='Resource_Vespene_Geyser'): #ans[shrinkScr(u.coord[0]-x+WINDOW_SIZE//2),shrinkScr(u.coord[1]-y+WINDOW_SIZE//2),4]=1 ans[shrinkScr(u.bounds[0] - x + WINDOW_SIZE // 2):shrinkScr(u.bounds[1] - x + WINDOW_SIZE // 2), shrinkScr(u.bounds[2] - y + WINDOW_SIZE // 2) : shrinkScr(u.bounds[3] - y + WINDOW_SIZE // 2), 4] = 1 else: ans[shrinkScr(u.bounds[0] - x + WINDOW_SIZE // 2):shrinkScr(u.bounds[1] - x + WINDOW_SIZE // 2), shrinkScr(u.bounds[2] - y + WINDOW_SIZE // 2): shrinkScr(u.bounds[3] - y + WINDOW_SIZE // 2), 3]=1 fstate=open('state.txt','wb') pickle.dump(ans, fstate) fstate.close() return ans @staticmethod def msg2mask(disGame, msg): x,y=msg.myInfo.coord X, Y = disGame.regions.shape ax=max(0,WINDOW_SIZE//2-x) ay=max(0,WINDOW_SIZE//2-y) ans=numpy.zeros([WINDOW_SIZE,WINDOW_SIZE,DroneNet._out_channel]) ans[WINDOW_SIZE//2,WINDOW_SIZE//2,0]=1 ans[ax:min(WINDOW_SIZE,X-x+WINDOW_SIZE//2), ay:min(WINDOW_SIZE,Y-y+WINDOW_SIZE//2),1]=disGame.regions[max(0,x-WINDOW_SIZE//2):min(x+WINDOW_SIZE//2,X), max(0,y-WINDOW_SIZE//2):min(y+WINDOW_SIZE//2,Y)] for u in msg.allies: if(u.type!='Zerg_Overlord'): top,bot,left,right=u.bounds ans[shrinkScr(top - x + WINDOW_SIZE // 2):shrinkScr(bot - x + WINDOW_SIZE // 2), shrinkScr(left - y + WINDOW_SIZE // 2):shrinkScr(right - x + WINDOW_SIZE // 2),1] = 0 for u in msg.resources: if(abs(u.coord[0] - x) < WINDOW_SIZE // 2 and abs(u.coord[1] - y) < WINDOW_SIZE // 2): top,bot,left,right=u.bounds ans[shrinkScr(top - x + WINDOW_SIZE // 2):shrinkScr(bot - x + WINDOW_SIZE // 2), shrinkScr(left - y + WINDOW_SIZE // 2):shrinkScr(right - x + WINDOW_SIZE // 2),1] = 0 fstate=open('mask.txt','wb') pickle.dump(ans, fstate) fstate.close() return ans
class Wide_ResNet(object): ''' Modified from: https://gist.github.com/kashif/0ba0270279a0f38280423754cea2ee1e ''' def __init__(self, config): self.verbose = config['verbose'] self.size = config['size'] self.rank = config['rank'] self.name = 'Wide_ResNet' # data from data.cifar10 import Cifar10_data self.data = Cifar10_data(verbose=False) self.build_model() # iter related self.current_t = 0 self.last_one_t=False self.current_v = 0 self.last_one_v=False self.n_subb = 1 self.n_epochs = nb_epoch self.epoch=0 def build_model(self): img_input = Input(shape=(img_channels, img_rows, img_cols)) # one conv at the beginning (spatial size: 32x32) x = ZeroPadding2D((1, 1))(img_input) x = Convolution2D(16, nb_row=3, nb_col=3)(x) # Stage 1 (spatial size: 32x32) x = bottleneck(x, n, 16, 16 * k, dropout=0.3, subsample=(1, 1)) # Stage 2 (spatial size: 16x16) x = bottleneck(x, n, 16 * k, 32 * k, dropout=0.3, subsample=(2, 2)) # Stage 3 (spatial size: 8x8) x = bottleneck(x, n, 32 * k, 64 * k, dropout=0.3, subsample=(2, 2)) x = BatchNormalization(mode=0, axis=1)(x) x = Activation('relu')(x) x = AveragePooling2D((8, 8), strides=(1, 1))(x) x = Flatten()(x) preds = Dense(nb_classes, activation='softmax')(x) self.model = Model(input=img_input, output=preds) self.keras_get_params() def compile_iter_fns(self, *args, **kwargs): try: sync_type = kwargs['sync_type'] except: raise RuntimeError('Keras wresnet needs sync_type keyword argument') if sync_type != 'avg': raise RuntimeError('currently wresnet only support compiling with sync_type avg, add BSP_SYNC_TYPE=avg in your session cfg') import time start = time.time() self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) self.shared_lr = self.model.optimizer.lr self.base_lr = self.shared_lr.get_value() if self.verbose: print('Compiling......') self.model._make_train_function() self.model._make_test_function() if self.verbose: print('Compile time: %.3f s' % (time.time()-start)) self.data.batch_data(self.model, batch_size) self.data.extend_data(rank=self.rank, size=self.size) self.data.shuffle_data(mode='train', common_seed=1234) self.data.shuffle_data(mode='val') self.data.shard_data(mode='train', rank=self.rank, size=self.size) # to update data.n_batch_train self.data.shard_data(mode='val', rank=self.rank, size=self.size) # to update data.n_batch_val def train_iter(self, count, recorder): if self.current_t ==0: self.data.shuffle_data(mode='train',common_seed=self.epoch) self.data.shard_data(mode='train',rank=self.rank, size=self.size) recorder.start() cost, acc = self.model.train_function(self.data.train_batches_shard[self.current_t]) recorder.train_error(count, cost, 1.0-acc) recorder.end('calc') if self.current_t == self.data.n_batch_train - 1: self.last_one_t = True else: self.last_one_t = False if self.last_one_t == False: self.current_t+=1 else: self.current_t=0 def val_iter(self, count, recorder): if self.current_v==0: self.data.shuffle_data(mode='val') self.data.shard_data(mode='val',rank=self.rank, size=self.size) cost, acc = self.model.test_function(self.data.val_batches_shard[self.current_v]) recorder.val_error(count, cost, 1.0-acc, 0) if self.current_v == self.data.n_batch_val - 1: self.last_one_v = True else: self.last_one_v = False if self.last_one_v == False: self.current_v+=1 else: self.current_v=0 def reset_iter(self, mode): '''used at the begininig of another mode''' if mode=='train': self.current_t = 0 self.subb_t=0 self.last_one_t = False else: self.current_v = 0 self.subb_v=0 self.last_one_v = False def adjust_hyperp(self, epoch): 'to be called once per epoch' if lr_policy == 'step': if epoch in lr_step: tuned_base_lr = self.shared_lr.get_value() /10. self.shared_lr.set_value(np.float32(tuned_base_lr)) def scale_lr(self, size): self.shared_lr.set_value(np.array(self.base_lr*size, dtype='float32')) def cleanup(self): pass def keras_get_params(self): self.params=[] for l in self.model.layers: self.params.extend(l.trainable_weights)