def get_model(name, config): if name == "densenet121": return DenseNet(config.num_classes, config.densenet_weights, config) elif name == "ResNet50": return ResNet50(config.num_classes, config.resnet_weights, config) else: raise NotImplementedError
def main(): train_loader, val_loader = create_train_val_dataloaders( args.train_data_fp, batch_size=args.batch_size) model = DenseNet(drop_prob=0) if use_cuda: model = model.cuda() train(model, train_loader, val_loader)
def main(): exp_name = f'baseline_{now()}' device, log, result_dir = setup(exp_name, conf) train_df = load_csv(conf.train_csv) if conf.npy: train_images = np.load(conf.train_images) else: train_images = pd.read_parquet(conf.train_images) test_df = load_csv(conf.test_csv) if conf.npy: test_images = np.load(conf.test_images) else: test_images = pd.read_parquet(conf.test_images) log.info('done') for i in range(5): if i != conf.fold: continue if "resnet" in conf.arch or "resnext" in conf.arch: model_ft = ResNet(conf, arch_name=conf.arch, input_size=conf.image_size) model_ft.load_state_dict( torch.load("result/baseline_2020_03_21_13_01_08/model_0.pkl")) elif "densenet" in conf.arch: model_ft = DenseNet(conf, arch_name=conf.arch, input_size=conf.image_size) elif "efficientnet" in conf.arch: model_ft = EfficientNet(conf, arch_name=conf.arch) criterion = [ nn.CrossEntropyLoss(reduction="none"), nn.CrossEntropyLoss(reduction="none"), nn.CrossEntropyLoss(reduction="none") ] criterion = [c.to(device) for c in criterion] model_ft, val_preds = train_model(train_df, train_images, test_df, test_images, model_ft, criterion, log, device, result_dir, fold=i, num_epoch=conf.num_epoch) torch.save(model_ft.state_dict(), result_dir / f'model_{i}.pkl') np.save(result_dir / f'val_preds_{i}.npy', val_preds)
def get_new_model(tmp_scale = True, num_classes = args.num_classes): if args.model == 'resnet18': return ResNet18(tmp_scale = tmp_scale, num_classes = num_classes) elif args.model == 'resnet50': return ResNet50(tmp_scale = tmp_scale, num_classes = num_classes) elif args.model == 'resnet101': return ResNet101(tmp_scale = tmp_scale, num_classes = num_classes) elif args.model == 'inceptionv4': return inceptionv4(tmp_scale = tmp_scale, num_classes = num_classes) elif args.model == 'densenet': return DenseNet(tmp_scale = tmp_scale)
def main(): # read csv files for driving log lines = [] with open('./data/data/driving_log.csv') as csvfile: reader = csv.reader(csvfile) for line in reader: lines.append(line) correction = 0.2 # This is a parameter to tune current_path = './data/data/' # modify the image path images = [] measurements = [] for line in lines[1:]: img_centre = line[0] img_left = line[1][1:] # get rid of the space at the beginning img_right = line[2][1:] # get rid of the space at the beginning # create adjusted steering meaurements for the side camera iamges steering_centre = float(line[3]) for num, img in enumerate([img_centre, img_left, img_right]): image = cv2.imread(current_path + img) images.append(image) images.append(cv2.flip(image, 1)) if num == 1: steering = steering_centre + correction elif num == 2: steering = steering_centre - correction elif num == 0: steering = steering_centre measurements.append(steering) measurements.append(steering * (-1.0)) X_train = np.array(images) y_train = np.array(measurements) # Load the model model = DenseNet(input_shape=(160, 320, 3), depth=3 * 3 + 4, nb_dense_block=3, growth_rate=12, bottleneck=True, reduction=0.5, weights=None, cropping=((70, 25), (0, 0)), norm_lambda=True, activation=None, classes=1) model.compile(loss='mse', optimizer='adam') model.fit(X_train, y_train, validation_split=0.2, shuffle=True, epochs=5) model.save('./models/model_densenet.h5')
def get_model(args): if args.model == 'simplenet': model = SimpleNet() elif args.model == 'densenet': model = DenseNet(block_lst=(4, 4, 4, 4)) else: raise ValueError if args.cuda: model.cuda() return model
def __init__(self, num_classes=10, combine_at='prepool', join_how='concat', multitask=True, drop_view_prob=0.0, architecture='densenet121'): super(MultiViewCNN, self).__init__() self.multitask = multitask self.drop_view_prob = [1 - drop_view_prob, drop_view_prob / 2., drop_view_prob / 2.] if multitask: # Never drop view when multitask # Use curriculum learning on loss instead self.drop_view_prob = [1., 0., 0.] self.combine_at = combine_at self.join_how = join_how params = {'in_channels': 1, 'num_classes': num_classes, **get_densenet_params(architecture)} self.frontal_model = DenseNet(**params) self.lateral_model = DenseNet(**params) self.joint_in_features = self.frontal_model.classifier.in_features if join_how == 'concat': self.joint_in_features *= 2 self.classifier = nn.Linear(in_features=self.joint_in_features, out_features=num_classes)
def tiny_densenet121(memory_efficient=False, **kwargs): r"""Tiny-Densenet-121 model from "Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf> The tiny-version has been specifically designed for neuroimaging data. It is 10X smaller than DenseNet. Args: memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient, but slower. Default: *False*. See `"paper" <https://arxiv.org/pdf/1707.06990.pdf>`_ """ return DenseNet(16, (6, 12, 16), 64, memory_efficient=memory_efficient, **kwargs)
def main(): dataset = Cifar10() # dataset = Cifar100() # dataset = Mnist() # model = AlexNet() # model = VGG() # model = GoogLeNet() # model = ResNet(model_type="101") # model = InceptionV3() model = DenseNet(model_type="201") # training trainer = ClfTrainer(model, dataset) trainer.run_training(epochs, batch_size, learning_rate, './cifar10-ckpt')
def __init__(self, pretrained=False): super(FECNet, self).__init__() growth_rate = 64 depth = 100 block_config = [5] efficient = True self.Inc = InceptionResnetV1(pretrained='vggface2', device='cuda').eval() for param in self.Inc.parameters(): param.requires_grad = False self.dense = DenseNet(growth_rate=growth_rate, block_config=block_config, num_classes=16, small_inputs=True, efficient=efficient, num_init_features=512).cuda() if (pretrained): load_weights(self)
def main(): config = Config() create_dirs([config.summary_dir, config.checkpoint_dir]) sess = tf.Session() train_data = Dataset(config.root, config.train_image_file, config.type, transform=Augmentaton(size=config.resize, mean=config.means[config.type], std=config.stds[config.type]), max_samples=None) valid_data = Dataset(config.root, config.valid_image_file, config.type, transform=Augmentaton(size=config.resize, mean=config.means[config.type], std=config.stds[config.type]), max_samples=None) train_data_loader = DataLoader(train_data) valid_data_loader = DataLoader(valid_data) model = DenseNet(config) logger = Logger(sess, config) trainer = DenseNetTrainer(sess, model, train_data_loader, valid_data_loader, config, logger) model.load(sess) if config.phase == "train": trainer.train() elif config.phase == "test": trainer.test("prediction.csv")
def get_model(args): if args.model == 'simplenet': model = SimpleNet() elif args.model == 'densenet': model = DenseNet(block_lst=(4, 4, 4, 4)) else: raise ValueError model_path = os.path.join(args.model_path, args.model + '/') os.makedirs(model_path, exist_ok=True) lossf = nn.BCEWithLogitsLoss(size_average=False) optimizer = optim.Adam(model.param_options(), weight_decay=args.l2) if args.model == 'simplenet': lrsched = LRSchedNone(optimizer.param_groups, 0.5e-3) elif args.model == 'densenet': lrsched = LRSchedNone(optimizer.param_groups, 1e-4) if args.cuda: model.cuda() lossf.cuda() return model, lossf, optimizer, lrsched, model_path
def evaluate(): config = Config() valid_data = Dataset(config.root, valid_image_paths, config.type, transform=Augmentaton(size=config.resize, mean=config.means[config.type], std=config.stds[config.type]), max_samples=10) valid_data_loader = DataLoader(valid_data) sess = tf.Session() model = DenseNet(config) logger = Logger(sess, config) trainer = DenseNetTrainer(sess, model, valid_data_loader, valid_data_loader, config, logger) model.load(sess) if config.phase == "train": trainer.train() elif config.phase == "test": trainer.test(output_prediction_path)
def model_fn(features, labels, mode, params): """Model function for DenseNet classifier. Args: features: inputs. labels: one hot encoded classes mode: one of tf.estimator.ModeKeys.{TRAIN, INFER, EVAL} params: a parameter dictionary with the following keys: Returns: ModelFnOps for Estimator API. """ number_classes = params.get('number_classes') growth_rate = params.get('growth_rate') dropout_keep_prob = params.get('dropout_keep_prob') encoder_num_units = params.get('encoder_num_units') decoder_num_units = params.get('decoder_num_units') bottleneck_number_feature_maps = params.get( 'bottleneck_number_feature_maps') densenet = DenseNet(growth_rate=growth_rate, dropout_keep_prob=dropout_keep_prob, number_classes=number_classes, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) densenet.encode( features=features, num_units=encoder_num_units, bottleneck_number_feature_maps=bottleneck_number_feature_maps) logits = densenet.decode(decoder_num_units).output predictions = tf.argmax(logits, axis=1) tf.summary.image('predictions', predictions) if mode == tf.estimator.ModeKeys.PREDICT: predictions = { 'class_ids': predictions, 'probabilities': tf.nn.softmax(logits), 'logits': logits, } return tf.estimator.EstimatorSpec(mode, predictions=predictions) # Add the loss. # Calculate loss, which includes softmax cross entropy and L2 regularization. # wraps the softmax_with_entropy fn. adds it to loss collection tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) # include the regulization losses in the loss collection. loss = tf.losses.get_total_loss() if mode == tf.estimator.ModeKeys.EVAL: return tf.estimator.EstimatorSpec(mode=mode, eval_metric_ops={ "accuracy": tf.metrics.accuracy( labels, predictions) }) assert mode == tf.estimator.ModeKeys.TRAIN global_step = tf.train.get_global_step() update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) optimizer = tf.train.AdagradOptimizer(learning_rate=0.1) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step=global_step) return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
def train_model(modname='alexnet', pm_ch='both', bs=16): """ Args: modname (string): Name of the model. Has to be one of the values: 'alexnet', batch 64 'densenet' 'inception' 'resnet', batch 16 'squeezenet', batch 16 'vgg' pm_ch (string): pixelmap channel -- 'time', 'charge', 'both', default to both """ # device configuration device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # hyper parameters max_epochs = 10 learning_rate = 0.001 # determine number of input channels nch = 2 if pm_ch != 'both': nch = 1 ds = PixelMapDataset('training_file_list.txt', pm_ch) # try out the data loader utility dl = torch.utils.data.DataLoader(dataset=ds, batch_size=bs, shuffle=True) # define model model = None if modname == 'alexnet': model = alexnet(num_classes=3, in_ch=nch).to(device) elif modname == 'densenet': model = DenseNet(num_classes=3, in_ch=nch).to(device) elif modname == 'inception': model = inception_v3(num_classes=3, in_ch=nch).to(device) elif modname == 'resnet': model = resnet18(num_classes=3, in_ch=nch).to(device) elif modname == 'squeezenet': model = squeezenet1_1(num_classes=3, in_ch=nch).to(device) elif modname == 'vgg': model = vgg19_bn(in_ch=nch, num_classes=3).to(device) else: print('Model {} not defined.'.format(modname)) return # loss and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # training process total_step = len(dl) for epoch in range(max_epochs): for i, (view1, view2, local_labels) in enumerate(dl): view1 = view1.float().to(device) if modname == 'inception': view1 = nn.ZeroPad2d((0, 192, 102, 101))(view1) else: view1 = nn.ZeroPad2d((0, 117, 64, 64))(view1) local_labels = local_labels.to(device) # forward pass outputs = model(view1) loss = criterion(outputs, local_labels) # backward and optimize optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % bs == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format( epoch + 1, max_epochs, i + 1, total_step, loss.item())) # save the model checkpoint save_path = '../../../data/two_views/saved_models/{}/{}'.format( modname, pm_ch) os.makedirs(save_path, exist_ok=True) torch.save(model.state_dict(), os.path.join(save_path, 'model.ckpt'))
patience = 50 base_path = '../trained_models/emotion_models/' # data generator data_generator = ImageDataGenerator(featurewise_center=False, featurewise_std_normalization=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, zoom_range=.1, horizontal_flip=True) # model parameters/compilation model = DenseNet(classes=num_classes, input_shape=(64, 64, 1), depth=40, growth_rate=12, bottleneck=True, reduction=0.5) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() datasets = ['fer2013'] for dataset_name in datasets: print('Training dataset:', dataset_name) # callbacks log_file_path = base_path + dataset_name + '_emotion_training.log' csv_logger = CSVLogger(log_file_path, append=False) early_stop = EarlyStopping('val_loss', patience=patience)
def main(GPU=True, epochs=20, lr=0.0001, momentum=0.9, weight_decay=1e-4, batch_size=32, start_epochs=0, model_name="msrn", optimizer="adam", naive=False): assert model_name.lower() in [ "msrn", "densenet", "densenet64" ], "Model not available only support msrn and densenet" discriminator = None model_name = model_name.lower() dense = True if model_name == "densenet" else False # loading all of the data training_data_loader = DataLoader(dataset=DataSets(dense=dense), batch_size=batch_size) testing_data_loader = DataLoader(dataset=DataSets(dense=dense, dataset='test'), batch_size=5) # validation_data_loader = DataLoader( # dataset=DataSets(dataset='val'), # batch_size=5) print("==================================================") # checking for model type print("Model: " + model_name + " with loss: ", end="") if model_name == "msrn": model = MSRN() criterion = nn.L1Loss(reduction='elementwise_mean') print(" L1 loss") elif model_name == "densenet64": model = DenseNet() discriminator = MSRN() criterion = nn.functional.nll_loss elif model_name == "densenet": model = DenseNet() # this can be tested with cross entropy # criterion = nn.CrossEntropyLoss criterion = nn.functional.nll_loss print(" negative Log loss") else: raise ValueError( "Invalid model_name not support {}".format(model_name)) # check for GPU support print("Using GPU: " + str(GPU)) if GPU: model = nn.DataParallel(model).cuda() if model_name == "msrn": criterion = criterion.cuda() else: model = model.cpu() print("Optimizer: " + optimizer + " with lr: " + str(lr)) # setting up optimizer if optimizer == "adam": optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay) elif optimizer == "sgd": # TODO add momentum flag optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay) elif optimizer == "rmsprop": optimizer = optim.RMSprop(model.parameters(), lr=lr, weight_decay=weight_decay) else: raise ValueError("Not supported Loss function") print("==================================================") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if model_name == 'msrn': MSRN().to(device).summary() elif model_name == 'densenet64': DenseNet(upscale=2).to(device).summary() else: DenseNet().to(device).summary() print("==================================================") log_folder = 'Logs/' + model_name # Loggering the training loss if not os.path.isdir(log_folder): os.makedirs(log_folder) model.name = model_name append_write = 'w' if os.path.exists(log_folder) else 'a' train_log = open(os.path.join(log_folder, 'train.csv'), append_write) test_log = open(os.path.join(log_folder, 'test.csv'), append_write) val_log = open(os.path.join(log_folder, 'val.csv'), append_write) if start_epochs > 0: model = load_model(model, model_name, start_epochs + 1) # Training in numbers of epochs for epoch in range(start_epochs, epochs): train(training_data_loader, optimizer, model, criterion, epoch, train_log, GPU, discriminator if discriminator else None, naive) # if running on 64` # densenet print('testing the model') if model_name == "densenet": test_dense(testing_data_loader, optimizer, model, criterion, epoch, test_log, GPU) else: test_msrn(testing_data_loader, optimizer, model, criterion, epoch, test_log, GPU, discriminator=discriminator if discriminator else None) save_checkpoint(model, epoch, model_name) train_log.close() test_log.close() val_log.close()
def __init__(self, p=None, h=None, use_word_embedding=True, word_embedding_weights=None, train_word_embeddings=False, dropout_init_keep_rate=1.0, dropout_decay_interval=10000, dropout_decay_rate=0.977, use_chars=False, chars_per_word=16, char_input_dim=100, char_embedding_size=8, char_conv_filters=100, char_conv_kernel_size=5, use_syntactical_features=False, syntactical_feature_size=50, use_exact_match=False, first_scale_down_ratio=0.3, nb_dense_blocks=3, layers_per_dense_block=8, nb_labels=3, growth_rate=20, transition_scale_down_ratio=0.5, inputs=None, outputs=None, name="DIIN"): """Densely Interactive Inference Network(DIIN) Model from paper `Natural Language Inference over Interaction Space` (https://openreview.net/forum?id=r1dHXnH6-¬eId=r1dHXnH6-) :param p: sequence length of premise :param h: sequence length of hypothesis :param use_word_embedding: whether or not to include word vectors in the model :param use_chars: whether or not to include character embeddings in the model :param use_syntactical_features: whether or not to include syntactical features (POS tags) in the model :param use_exact_match: whether or not to include exact match features in the model :param word_embedding_weights: matrix of weights for word embeddings(pre-trained vectors) :param train_word_embeddings: whether or not to modify word embeddings while training :param dropout_init_keep_rate: initial keep rate of dropout :param dropout_decay_interval: the number of steps to wait for the next turn update, steps means single batch, other than epoch :param dropout_decay_rate: how much to change dropout at each interval :param chars_per_word: how many chars are there per one word :param char_input_dim: character unique numbers :param char_embedding_size: output size of the character-embedding layer :param char_conv_filters: filters of the kernel applied on character embeddings :param char_conv_kernel_size: size of the kernel applied on character embeddings :param syntactical_feature_size: size of the syntactical feature vector for each word :param first_scale_down_ratio: scale ratio of map features as the input of first Densenet block :param nb_dense_blocks: number of dense blocks in densenet :param layers_per_dense_block: number of layers in one dense block :param nb_labels: number of labels :param growth_rate:growing rate in dense net :param transition_scale_down_ratio: transition scale down ratio in dense net :param inputs: inputs of keras models :param outputs: outputs of keras models :param name: models name """ if inputs or outputs: super(DIINModel, self).__init__(inputs=inputs, outputs=outputs, name=name) return if use_word_embedding: assert word_embedding_weights is not None, "Word embedding weights are needed" inputs = [] premise_features = [] hypothesis_features = [] """Embedding layer""" # Input: word embedding if use_word_embedding: premise_word_input = Input(shape=(p, ), dtype="int64", name="premise_word_input") hypothesis_word_input = Input(shape=(h, ), dtype="int64", name="hypothesis_word_input") inputs.append(premise_word_input) inputs.append(hypothesis_word_input) word_embedding = Embedding( input_dim=word_embedding_weights.shape[0], output_dim=word_embedding_weights.shape[1], weights=[word_embedding_weights], trainable=train_word_embeddings, name="word_embedding") premise_word_embedding = word_embedding(premise_word_input) hypothesis_word_embedding = word_embedding(hypothesis_word_input) premise_word_embedding = DecayingDropout( init_keep_rate=dropout_init_keep_rate, decay_interval=dropout_decay_interval, decay_rate=dropout_decay_rate, name="premise_word_dropout")(premise_word_embedding) hypothesis_word_embedding = DecayingDropout( init_keep_rate=dropout_init_keep_rate, decay_interval=dropout_decay_interval, decay_rate=dropout_decay_rate, name="hypothesis_word_dropout")(hypothesis_word_embedding) premise_features.append(premise_word_embedding) hypothesis_features.append(hypothesis_word_embedding) # Input: character embedding if use_chars: premise_char_input = Input(shape=(p, chars_per_word), dtype="int64", name="premise_char_input") hypothesis_char_input = Input(shape=(h, chars_per_word), dtype="int64", name="hypothesis_char_input") inputs.append(premise_char_input) inputs.append(hypothesis_char_input) # Share weights of character-level embedding for premise and hypothesis character_embedding = TimeDistributed(Sequential([ Embedding(input_dim=char_input_dim, output_dim=char_embedding_size, input_length=chars_per_word), Conv1D(filters=char_conv_filters, kernel_size=char_conv_kernel_size), GlobalMaxPooling1D(), ]), name="char_embedding") character_embedding.build( input_shape=(None, None, chars_per_word)) # Set input shape premise_char_embedding = character_embedding(premise_char_input) hypothesis_char_embedding = character_embedding( hypothesis_char_input) premise_features.append(premise_char_embedding) hypothesis_features.append(hypothesis_char_embedding) # Input: syntactical features if use_syntactical_features: premise_syntactical_input = Input(shape=(p, syntactical_feature_size), name="premise_syntactical_input") hypothesis_syntactical_input = Input( shape=(h, syntactical_feature_size), name="hypothesis_syntactical_input") inputs.append(premise_syntactical_input) inputs.append(hypothesis_syntactical_input) premise_features.append(premise_syntactical_input) hypothesis_features.append(hypothesis_syntactical_input) # Input: one-hot exact match feature if use_exact_match: premise_exact_match_input = Input(shape=(p, ), name='premise_exact_match_input') hypothesis_exact_match_input = Input( shape=(h, ), name='hypothesis_exact_match_input') inputs.append(premise_exact_match_input) inputs.append(hypothesis_exact_match_input) premise_exact_match = Reshape( target_shape=(p, 1))(premise_exact_match_input) hypothesis_exact_match = Reshape( target_shape=(h, 1))(hypothesis_exact_match_input) premise_features.append(premise_exact_match) hypothesis_features.append(hypothesis_exact_match) # Concatenate all features if len(premise_features) > 1: premise_embedding = Concatenate()(premise_features) hypothesis_embedding = Concatenate()(hypothesis_features) else: premise_embedding = premise_features[0] hypothesis_embedding = hypothesis_features[0] d = K.int_shape(premise_embedding)[-1] """Encoding layer""" premise_encoding = Encoding(name="premise_encoding")(premise_embedding) hypothesis_encoding = Encoding( name="hypothesis_encoding")(hypothesis_embedding) """Interaction layer""" interaction = Interaction(name="interaction")( [premise_encoding, hypothesis_encoding]) """Feature extraction layer""" feature_extractor_input = Conv2D( filters=int(d * first_scale_down_ratio), kernel_size=1, activation=None, name="bottleneck")(interaction) # Bottleneck layer feature_extractor = DenseNet( input_tensor=Input(shape=K.int_shape(feature_extractor_input)[1:]), include_top=False, nb_dense_block=nb_dense_blocks, nb_layers_per_block=layers_per_dense_block, growth_rate=growth_rate, compression=transition_scale_down_ratio)(feature_extractor_input) """Output layer""" features = DecayingDropout(init_keep_rate=dropout_init_keep_rate, decay_interval=dropout_decay_interval, decay_rate=dropout_decay_rate, name="features")(feature_extractor) if nb_labels == 2: out = Dense(1, activation="sigmoid", name="output")(features) else: out = Dense(nb_labels, activation="softmax", name="output")(features) super(DIINModel, self).__init__(inputs=inputs, outputs=out, name=name)
helper.compute_rdp() if helper.params['dataset'] == 'cifar10': num_classes = 10 elif helper.params['dataset'] == 'cifar100': num_classes = 100 elif helper.params['dataset'] == 'inat': num_classes = len(helper.labels) logger.info('num class: ', num_classes) elif helper.params['dataset'] == 'dif': num_classes = len(helper.labels) else: num_classes = 10 reseed(5) if helper.params['model'] == 'densenet': net = DenseNet(num_classes=num_classes, depth=helper.params['densenet_depth']) elif helper.params['model'] == 'resnet': logger.info(f'Model size: {num_classes}') net = models.resnet18(num_classes=num_classes) elif helper.params['model'] == 'PretrainedRes': net = models.resnet18(pretrained=True) net.fc = nn.Linear(512, num_classes) net = net.cuda() elif helper.params['model'] == 'FlexiNet': net = FlexiNet(3, num_classes) elif helper.params['model'] == 'dif_inception': net = inception_v3(pretrained=True, dif=True) net.fc = nn.Linear(768, num_classes) net.aux_logits = False elif helper.params['model'] == 'inception': net = inception_v3(pretrained=True)
def test_model(modname='alexnet', pm_ch='both', bs=16): # hyperparameters batch_size = bs # device configuration device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # determine number of input channels nch = 2 if pm_ch != 'both': nch = 1 # restore model model = None if modname == 'alexnet': model = alexnet(num_classes=3, in_ch=nch).to(device) elif modname == 'densenet': model = DenseNet(num_classes=3, in_ch=nch).to(device) elif modname == 'inception': model = inception_v3(num_classes=3, in_ch=nch).to(device) elif modname == 'resnet': model = resnet18(num_classes=3, in_ch=nch).to(device) elif modname == 'squeezenet': model = squeezenet1_1(num_classes=3, in_ch=nch).to(device) elif modname == 'vgg': model = vgg19_bn(in_ch=nch, num_classes=3).to(device) else: print('Model {} not defined.'.format(modname)) return # retrieve trained model # load path load_path = '../../../data/two_views/saved_models/{}/{}'.format( modname, pm_ch) model_pathname = os.path.join(load_path, 'model.ckpt') if not os.path.exists(model_pathname): print('Trained model file {} does not exist. Abort.'.format( model_pathname)) return model.load_state_dict(torch.load(model_pathname)) # load test dataset test_dataset = PixelMapDataset('test_file_list.txt', pm_ch) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) # test the model model.eval( ) # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance) with torch.no_grad(): correct = 0 total = 0 correct_cc_or_bkg = 0 ws_total = 0 ws_correct = 0 for view1, view2, labels in test_loader: view1 = view1.float().to(device) if modname == 'inception': view1 = nn.ZeroPad2d((0, 192, 102, 101))(view1) else: view1 = nn.ZeroPad2d((0, 117, 64, 64))(view1) labels = labels.to(device) outputs = model(view1) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() for i in range(len(predicted)): if (predicted[i] < 2 and labels[i] < 2) or (predicted[i] == 2 and labels[i] == 2): correct_cc_or_bkg += 1 if labels[i] < 2: ws_total += 1 if (predicted[i] == labels[i]): ws_correct += 1 print('Model Performance:') print('Model:', modname) print('Channel:', pm_ch) print( '3-class Test Accuracy of the model on the test images: {}/{}, {:.2f} %' .format(correct, total, 100 * correct / total)) print( '2-class Test Accuracy of the model on the test images: {}/{}, {:.2f} %' .format(correct_cc_or_bkg, total, 100 * correct_cc_or_bkg / total)) print( 'Wrong-sign Test Accuracy of the model on the test images: {}/{}, {:.2f} %' .format(ws_correct, ws_total, 100 * ws_correct / ws_total))
elif args.model == 'resnet': from models.resnet import ResNet net = ResNet({ 'input_shape': (1, 3, 32, 32), 'n_classes': num_classes, 'base_channels': 16, 'block_type': 'basic', 'depth': 20 }) elif args.model == 'densenet': from models.densenet import DenseNet net = DenseNet({ 'input_shape': (1, 3, 32, 32), 'n_classes': num_classes, "depth": 40, "block_type": "bottleneck", "growth_rate": 24, "drop_rate": 0.0, "compression_rate": 1 }) # 1 is turns compression off start_epoch = 0 # Restore model if args.load != '': for i in range(1000 - 1, -1, -1): model_name = os.path.join(args.load, args.dataset + '_epoch_' + str(i) + '.pt') if os.path.isfile(model_name): net.load_state_dict(torch.load(model_name)) print('Model restored! Epoch:', i) start_epoch = i + 1
test = list(test_loader) test = list(zip(*test)) X_test = torch.cat(test[0], 0) y_test = torch.cat(test[1], 0) data = { 'X_train': X_train, 'y_train': y_train, 'X_val': X_val, 'y_val': y_val, 'X_test': X_test, 'y_test': y_test } model = DenseNet(input_param=(1, 64), block_layers=(6, 4), num_classes=10, growth_rate=32, bn_size=2, dropout_rate=0, transition_pool_param=(3, 1, 1)) loss_fn = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), weight_decay=1e-4) solver = Solver(model, data, optimizer, loss_fn) solver.train(num_epoch=2, file_prefix='mnist-') solver.predict(file_prefix='mnist-')
def model_fn(features, labels, mode, params): """Model function for DenseNet classifier. Args: features: inputs. labels: one hot encoded classes mode: one of tf.estimator.ModeKeys.{TRAIN, INFER, EVAL} params: a parameter dictionary with the following keys: Returns: ModelFnOps for Estimator API. """ number_classes = params.get('number_classes') growth_rate = params.get('growth_rate') dropout_keep_prob = params.get('dropout_keep_prob') encoder_num_units = params.get('encoder_num_units') decoder_num_units = params.get('decoder_num_units') bottleneck_number_feature_maps = params.get( 'bottleneck_number_feature_maps') tf.logging.info("features tensor {}".format(features)) features, image_ids, image_shapes = features['image'], features[ 'image_id'], features['image_shape'] densenet = DenseNet(growth_rate=growth_rate, dropout_keep_prob=dropout_keep_prob, number_classes=number_classes, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) densenet.encode( features=features, num_units=encoder_num_units, bottleneck_number_feature_maps=bottleneck_number_feature_maps) logits = densenet.decode(decoder_num_units).output probs = tf.nn.softmax(logits) predictions = tf.argmax(probs, axis=-1) if mode == tf.estimator.ModeKeys.PREDICT: # resize the predictions back to original size. # label_shape = tf.shape(features)[:2] probs = tf.image.resize_bilinear(probs, image_shapes, name='resize_predictions') predictions = tf.argmax(probs, axis=-1) tf.logging.info("Starting to predict..") predictions = { 'class_ids': predictions, 'probabilities': probs, 'logits': logits, 'image_ids': image_ids } tf.logging.info("prediction tensor {}".format(predictions)) return tf.estimator.EstimatorSpec(mode, predictions=predictions) # Add the loss. # Calculate loss, which includes softmax cross entropy and L2 regularization. # wraps the softmax_with_entropy fn. adds it to loss collection tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) # include the regulization losses in the loss collection. loss = tf.losses.get_total_loss() if mode == tf.estimator.ModeKeys.EVAL: tf.logging.info("Starting to evaluate..") with tf.variable_scope('mean_iou_calc'): prec = [] up_opts = [] for t in np.arange(0.5, 1.0, 0.05): predicted_mask = tf.to_int32(probs > t) score, up_opt = tf.metrics.mean_iou(labels, predicted_mask, 2) up_opts.append(up_opt) prec.append(score) mean_iou = tf.reduce_mean(tf.stack(prec), axis=0), tf.stack(up_opts) eval_metrics = {'mean_iou': mean_iou} return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metrics) assert mode == tf.estimator.ModeKeys.TRAIN tf.logging.info("Starting to train..") global_step = tf.train.get_global_step() update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) optimizer = tf.train.AdagradOptimizer(learning_rate=1e-4) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step=global_step) add_summaries(predictions, features, loss) return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
train_loader = DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=args.num_worker) test_loader = DataLoader(dataset_test, batch_size=args.batch_size_test, shuffle=False, num_workers=args.num_worker) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') print('==> Making model..') net = DenseNet(growth_rate=args.growth_rate, theta=args.theta, num_layers=[12, 12, 12], num_classes=10) net = net.to(device) if device == 'cuda': net = torch.nn.DataParallel(net) cudnn.benchmark = True num_params = sum(p.numel() for p in net.parameters() if p.requires_grad) print('The number of parameters of model is', num_params) if args.resume is not None: checkpoint = torch.load('./save_model/' + args.resume) net.load_state_dict(checkpoint['net']) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(),