Esempio n. 1
0
 def summarize(self):
     # Print summary of model
     summary(self, (3, 32, 32))
     return
Esempio n. 2
0
 def print_summary(self):
     print('Model Summary')
     summary(self, input_size=(3, 224, 224))
     print('\n')
Esempio n. 3
0
def test():
    model = UNetResNetV6(34, num_filters=32, inchannel=3).cuda()
    # inputs = torch.randn(2,3,128,128)
    summary(model, (3, 128, 128))
Esempio n. 4
0
dataset = PlanDataset(root_dir="data/deep_cardinality")
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)

train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size

# train_temp = [dataset[i] for i in range(10)]
# test_temp = [dataset[i] for i in range(5)]

train_dataset, test_dataset = random_split(dataset, [train_size, test_size])
# train_loader = DataLoader(train_dataset, batch_size=1, shuffle=True, num_workers=2)
# test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=2)

encoder = Encoder(d_feature=9 + 6 + 64, d_model=256, d_ff=128, N=4).double()
summary(encoder)

criterion = nn.MSELoss()
optimizer = optim.Adam(encoder.parameters(), lr=0.001)

epoch_size = 2


def train():
    result = []
    for epoch in range(epoch_size):
        print("epoch : ", epoch)
        running_loss = 0.0
        for i, data in enumerate(train_dataset):
            tree, nodemat, leafmat, label = data
            optimizer.zero_grad()
import torch
from torchsummary import summary
from nets.yolo3 import YoloBody
from utils.config import Config

if __name__ == "__main__":
    # 需要使用device来指定网络在GPU还是CPU运行
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    config = {
        "model_params": {
            "backbone_name": "darknet_53"
        },
        "yolo": {
            "anchors": [[1, 2, 3], [2, 3, 4], [3, 4, 5]],
            "classes": 80
        }
    }
    m = YoloBody(config).to(device)
    summary(m, input_size=(3, 416, 416))
Esempio n. 6
0
            nn.Linear(500, 500),
            nn.BatchNorm1d(500),
            nn.ReLU(),
            # ------------------------------------------------------
            nn.Linear(500, 500),
            nn.BatchNorm1d(500),
            nn.ReLU(),
            # ------------------------------------------------------
            nn.Linear(500, 50),
            nn.BatchNorm1d(50),
            nn.ReLU(),
            # ------------------------------------------------------
            nn.Linear(50, out_num),
            nn.ReLU6())

    def forward(self, x):
        x = self.FC(x)
        return x / 6


if __name__ == '__main__':
    import torchsummary

    if torch.cuda.is_available():
        generator = GeneratorNet().cuda()
    else:
        generator = GeneratorNet()
    params = list(generator.parameters())
    print(len(params))
    torchsummary.summary(generator, tuple([29]))
def train(args):
    # Setup Dataloader ============================================================
    data_loader_seg = get_loader(args.dataset)
    data_path = get_data_path(args.dataset)
    v_loader = data_loader_seg(data_path, is_transform=True, split='val', img_size=(args.img_rows, args.img_cols), \
                               img_norm=args.img_norm)
    n_classes = v_loader.n_classes
    valloader = data.DataLoader(v_loader, batch_size=1, num_workers=8)   \
        # IN VALIDATION WE CALCULATE ALL OF THE IMAGES ONE BY ONE => MADE BATCH SIZE 1 PREVIOUSLY IT WAS BATCHSIZE

    # Setup Metrics =================================
    running_metrics = runningScore(n_classes)
    # Setup Model=======================================================================================================
    model_features = get_model('resnet', n_classes)
    model_segment = get_model('segment', n_classes)
    model_reconstruct = get_model('reconstruct', n_classes=3)
    model_depth = get_model('depth', n_classes=1)
    #move model to cuda
    model_features = torch.nn.DataParallel(model_features,
                                           device_ids=range(
                                               torch.cuda.device_count()))
    model_features.cuda()
    model_segment = torch.nn.DataParallel(model_segment,
                                          device_ids=range(
                                              torch.cuda.device_count()))
    model_segment.cuda()
    model_reconstruct = torch.nn.DataParallel(model_reconstruct,
                                              device_ids=range(
                                                  torch.cuda.device_count()))
    model_reconstruct.cuda()
    model_depth = torch.nn.DataParallel(model_depth,
                                        device_ids=range(
                                            torch.cuda.device_count()))
    model_depth.cuda()
    #Initializer.initialize(model=model_segment, initialization=init.xavier_uniform)
    #print the model outputs
    summary(model_features, (3, args.img_rows, args.img_cols))
    summary(model_segment, (512, 8, 16))
    #summary(model_reconstruct, (512, 8, 16))
    #summary(model_classify, (512, 8, 16))

    # Load the pretrained =============================================================================================
    if args.resume_feature:
        print("Loading model and optimizer from checkpoint '{}'".format(
            args.resume_feature))
        # FEATURE NETWORK
        checkpoint_feature = torch.load(args.resume_feature)
        model_features.load_state_dict(checkpoint_feature['model_state'])
        print("Loaded checkpoint '{}' (epoch {})".format(
            args.resume_feature, checkpoint_feature['epoch']))
    if args.resume_segment:
        print("Loading model and optimizer from checkpoint '{}'".format(
            args.resume_segment))
        # SEGMENTATION NETWORK
        checkpoint_segment = torch.load(args.resume_segment)
        model_segment.load_state_dict(checkpoint_segment['model_state'],
                                      strict=False)
        print("Loaded checkpoint '{}' (epoch {})".format(
            args.resume_segment, checkpoint_segment['epoch']))
    if args.resume_reconstruct:
        print("Loading model and optimizer from checkpoint '{}'".format(
            args.resume_reconstruct))
        # RECONSTRUCTION NETWORK
        checkpoint_reconstruct = torch.load(args.resume_reconstruct)
        model_reconstruct.load_state_dict(
            checkpoint_reconstruct['model_state'], strict=False)
        print("Loaded checkpoint '{}' (epoch {})".format(
            args.resume_reconstruct, checkpoint_reconstruct['epoch']))
    if args.resume_depth:
        # DEPTH NETWORK
        checkpoint_depth = torch.load(args.resume_depth)
        model_depth.load_state_dict(checkpoint_depth['model_state'],
                                    strict=False)
        print("Loaded checkpoint '{}' (epoch {})".format(
            args.resume_depth, checkpoint_depth['epoch']))

    png_jpg_flag = args.quality  #0 means PNG, 95, 90, 85, 80 mean the JPG quantization
    # START Testing ===================================================================================================
    model_features.eval()
    model_segment.eval()
    model_reconstruct.eval()
    model_depth.eval()

    for i_val, (images_val, labels_val, obj_label_val,
                images_val_not_normalized,
                depth_val) in (enumerate(valloader)):  #tqdm
        images_val = Variable(images_val.cuda(), volatile=True)
        images_val_not_normalized = Variable(images_val_not_normalized.cuda(),
                                             volatile=True)
        labels_val = Variable(labels_val.cuda(), volatile=True)
        depth_val = Variable(depth_val.cuda(), volatile=True)

        #feed forward -> features
        features_val = model_features(images_val)
        # Quantization (returns the integer features in the range of 0-255)
        features_val, features_val_255, max_to_encode, min_to_encode = Quantize_center_VALIDATION(
            features_val)
        features_val_255_numpy = features_val_255.data.cpu().numpy()

        #Dumping
        featue_file_name = './dumped_features/temp/val_comp' + str(
            i_val)  # +'.bin'
        features_val = dump_feature_2D(features_val_255_numpy,
                                       featue_file_name, max_to_encode,
                                       min_to_encode, png_jpg_flag)
        #==========================================================

        # feed forward -> Tasks
        outputs_segment_val = model_segment(features_val)
        outputs_reconstruct_val = model_reconstruct(features_val)
        outputs_depth_val = model_depth(features_val)

        gt_label = labels_val.data.cpu().numpy()
        pred_label = outputs_segment_val.data.max(1)[1].cpu().numpy()

        depth_val_gt = depth_val.data.cpu().numpy()
        depth_val_pred = outputs_depth_val.data.cpu().numpy()
        depth_val_pred = depth_val_pred[0, :, :, :]

        # Visualize
        if args.visualize:
            #depth_val_pred   = depth_val_pred[0,:,:,:]
            depth_val_pred_plt = depth_val_pred.transpose((1, 2, 0))
            depth_val_pred_plt = depth_val_pred_plt[:, :,
                                                    0]  #np.squeeze(depth_val_pred, axis=2)
            depth_val_gt_plt = depth_val_gt.transpose((1, 2, 0))
            depth_val_gt_plt = depth_val_gt_plt[:, :, 0]

            images_debug = images_val_not_normalized.data.cpu().numpy()
            images_debug = images_debug[0, :, :, :]
            images_debug = images_debug.transpose((1, 2, 0))
            outputs_reconstruct_val_clamp = torch.clamp(
                outputs_reconstruct_val, 0.0, 1.0)
            recons_debug = outputs_reconstruct_val_clamp.data.cpu().numpy()
            recons_debug = recons_debug[0, :, :, :]
            recons_debug = recons_debug.transpose((1, 2, 0))

            seg_pred_color = decode_segmap_mine(pred_label[0, :, :])
            seg_gt_color = decode_segmap_mine(gt_label[0, :, :])
            f2, axar = plt.subplots(1, 6)
            axar[0].imshow((images_debug * 255).astype(np.uint8))
            axar[0].axis('off')
            axar[1].imshow(seg_gt_color)
            axar[1].axis('off')
            axar[2].imshow(((depth_val_gt_plt).astype(np.int32)),
                           cmap=cm.coolwarm)
            axar[2].axis('off')
            axar[3].imshow(recons_debug)
            axar[3].axis('off')
            axar[4].imshow(seg_pred_color)
            axar[4].axis('off')
            axar[5].imshow(((depth_val_pred_plt).astype(np.int32)),
                           cmap=cm.coolwarm)
            axar[5].axis('off')
            f2.savefig('./dumped_images/val_figure' + str(i_val) + '.png',
                       dpi=300)
            #plt.show()

        running_metrics.update(gt_label, pred_label,\
                                   images_val_not_normalized , outputs_reconstruct_val,
                                depth_val_gt,depth_val_pred)
    score, class_iou = running_metrics.get_scores()
    for k, v in score.items():
        print(k, v)
    running_metrics.reset()
Esempio n. 8
0
 def test_summary(self):
     print(summary(self.model, (3, 64, 64), device="cpu"))
Esempio n. 9
0
def run(args):
    with open(args.cfg_path) as f:
        cfg = edict(json.load(f))
        if args.verbose is True:
            print(json.dumps(cfg, indent=4))

    if not os.path.exists(args.save_path):
        os.mkdir(args.save_path)
    if args.logtofile is True:
        logging.basicConfig(filename=args.save_path + '/log.txt',
                            filemode="w",
                            level=logging.INFO)
    else:
        logging.basicConfig(level=logging.INFO)

    if not args.resume:
        with open(os.path.join(args.save_path, 'cfg.json'), 'w') as f:
            json.dump(cfg, f, indent=1)

    device_ids = list(map(int, args.device_ids.split(',')))
    num_devices = torch.cuda.device_count()
    if num_devices < len(device_ids):
        raise Exception('#available gpu : {} < --device_ids : {}'.format(
            num_devices, len(device_ids)))
    device = torch.device('cuda:{}'.format(device_ids[0]))

    model = Classifier(cfg)
    if args.verbose is True:
        from torchsummary import summary
        if cfg.fix_ratio:
            h, w = cfg.long_side, cfg.long_side
        else:
            h, w = cfg.height, cfg.width
        summary(model.to(device), (3, h, w))
    model = DataParallel(model, device_ids=device_ids).to(device).train()
    if args.pre_train is not None:
        if os.path.exists(args.pre_train):
            ckpt = torch.load(args.pre_train, map_location=device)
            model.module.load_state_dict(ckpt)
    optimizer = get_optimizer(model.parameters(), cfg)

    src_folder = os.path.dirname(os.path.abspath(__file__)) + '/../'
    dst_folder = os.path.join(args.save_path, 'classification')
    rc, size = subprocess.getstatusoutput('du --max-depth=0 %s | cut -f1' %
                                          src_folder)
    if rc != 0:
        raise Exception('Copy folder error : {}'.format(rc))
    rc, err_msg = subprocess.getstatusoutput('cp -R %s %s' %
                                             (src_folder, dst_folder))
    if rc != 0:
        raise Exception('copy folder error : {}'.format(err_msg))

    copyfile(cfg.train_csv, os.path.join(args.save_path, 'train.csv'))
    copyfile(cfg.dev_csv, os.path.join(args.save_path, 'dev.csv'))

    dataloader_train = DataLoader(ImageDataset(cfg.train_csv,
                                               cfg,
                                               mode='train'),
                                  batch_size=cfg.train_batch_size,
                                  num_workers=args.num_workers,
                                  drop_last=True,
                                  shuffle=True)
    dataloader_dev = DataLoader(ImageDataset(cfg.dev_csv, cfg, mode='dev'),
                                batch_size=cfg.dev_batch_size,
                                num_workers=args.num_workers,
                                drop_last=False,
                                shuffle=False)
    dev_header = dataloader_dev.dataset._label_header

    summary_train = {'epoch': 0, 'step': 0}
    summary_dev = {'loss': float('inf'), 'acc': 0.0}
    summary_writer = SummaryWriter(args.save_path)
    epoch_start = 0
    best_dict = {
        "acc_dev_best": 0.0,
        "auc_dev_best": 0.0,
        "loss_dev_best": float('inf'),
        "fused_dev_best": 0.0,
        "best_idx": 1
    }

    if args.resume:
        ckpt_path = os.path.join(args.save_path, 'train.ckpt')
        ckpt = torch.load(ckpt_path, map_location=device)
        model.module.load_state_dict(ckpt['state_dict'])
        summary_train = {'epoch': ckpt['epoch'], 'step': ckpt['step']}
        best_dict['acc_dev_best'] = ckpt['acc_dev_best']
        best_dict['loss_dev_best'] = ckpt['loss_dev_best']
        best_dict['auc_dev_best'] = ckpt['auc_dev_best']
        epoch_start = ckpt['epoch']

    for epoch in range(epoch_start, cfg.epoch):
        lr = lr_schedule(cfg.lr, cfg.lr_factor, summary_train['epoch'],
                         cfg.lr_epochs)
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

        summary_train, best_dict = train_epoch(summary_train, summary_dev, cfg,
                                               args, model, dataloader_train,
                                               dataloader_dev, optimizer,
                                               summary_writer, best_dict,
                                               dev_header)

        time_now = time.time()
        summary_dev, predlist, true_list = test_epoch(summary_dev, cfg, args,
                                                      model, dataloader_dev)
        time_spent = time.time() - time_now

        auclist = []
        for i in range(len(cfg.num_classes)):
            y_pred = predlist[i]
            y_true = true_list[i]
            fpr, tpr, thresholds = metrics.roc_curve(y_true,
                                                     y_pred,
                                                     pos_label=1)
            auc = metrics.auc(fpr, tpr)
            auclist.append(auc)
        summary_dev['auc'] = np.array(auclist)

        loss_dev_str = ' '.join(
            map(lambda x: '{:.5f}'.format(x), summary_dev['loss']))
        acc_dev_str = ' '.join(
            map(lambda x: '{:.3f}'.format(x), summary_dev['acc']))
        auc_dev_str = ' '.join(
            map(lambda x: '{:.3f}'.format(x), summary_dev['auc']))

        logging.info('{}, Dev, Step : {}, Loss : {}, Acc : {}, Auc : {},'
                     'Mean auc: {:.3f} '
                     'Run Time : {:.2f} sec'.format(
                         time.strftime("%Y-%m-%d %H:%M:%S"),
                         summary_train['step'], loss_dev_str, acc_dev_str,
                         auc_dev_str, summary_dev['auc'].mean(), time_spent))

        for t in range(len(cfg.num_classes)):
            summary_writer.add_scalar('dev/loss_{}'.format(dev_header[t]),
                                      summary_dev['loss'][t],
                                      summary_train['step'])
            summary_writer.add_scalar('dev/acc_{}'.format(dev_header[t]),
                                      summary_dev['acc'][t],
                                      summary_train['step'])
            summary_writer.add_scalar('dev/auc_{}'.format(dev_header[t]),
                                      summary_dev['auc'][t],
                                      summary_train['step'])

        save_best = False

        mean_acc = summary_dev['acc'][cfg.save_index].mean()
        if mean_acc >= best_dict['acc_dev_best']:
            best_dict['acc_dev_best'] = mean_acc
            if cfg.best_target == 'acc':
                save_best = True

        mean_auc = summary_dev['auc'][cfg.save_index].mean()
        if mean_auc >= best_dict['auc_dev_best']:
            best_dict['auc_dev_best'] = mean_auc
            if cfg.best_target == 'auc':
                save_best = True

        mean_loss = summary_dev['loss'][cfg.save_index].mean()
        if mean_loss <= best_dict['loss_dev_best']:
            best_dict['loss_dev_best'] = mean_loss
            if cfg.best_target == 'loss':
                save_best = True

        if save_best:
            torch.save(
                {
                    'epoch': summary_train['epoch'],
                    'step': summary_train['step'],
                    'acc_dev_best': best_dict['acc_dev_best'],
                    'auc_dev_best': best_dict['auc_dev_best'],
                    'loss_dev_best': best_dict['loss_dev_best'],
                    'state_dict': model.module.state_dict()
                },
                os.path.join(args.save_path,
                             'best{}.ckpt'.format(best_dict['best_idx'])))
            best_dict['best_idx'] += 1
            if best_dict['best_idx'] > cfg.save_top_k:
                best_dict['best_idx'] = 1
            logging.info('{}, Best, Step : {}, Loss : {}, Acc : {},'
                         'Auc :{},Best Auc : {:.3f}'.format(
                             time.strftime("%Y-%m-%d %H:%M:%S"),
                             summary_train['step'], loss_dev_str, acc_dev_str,
                             auc_dev_str, best_dict['auc_dev_best']))
        torch.save(
            {
                'epoch': summary_train['epoch'],
                'step': summary_train['step'],
                'acc_dev_best': best_dict['acc_dev_best'],
                'auc_dev_best': best_dict['auc_dev_best'],
                'loss_dev_best': best_dict['loss_dev_best'],
                'state_dict': model.module.state_dict()
            }, os.path.join(args.save_path, 'train.ckpt'))
    summary_writer.close()
Esempio n. 10
0
    x = F.avg_pool2d(x,4)
    # tensor.view() is like reshape() with the size -1 is inferred from other dimensions
    # e.g. (4,4) --->view(16,-1) == (16,1) --->view(8,-1) == (8,2)
    # basically it's like x.flatten() == x.view(x.size(0),-1)
    x = x.view(x.size(0),-1)
    x = self.fc(x)
    return x

# define model
model = ResNet(ResidualBlock,[2,2,2,2]).to(device)

"""# model summary"""

from torchsummary import summary

summary(model.cpu(),input_size=(3,32,32))

# loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)

# updating learning rate
def update_lr(optimizer,lr):
  for param_group in optimizer.param_groups:
    param_group['lr'] = lr 

# train model
per_epoch_total_step = len(trainloader)
current_lr = learning_rate
sum_loss = 0.0
total_label = 0
Esempio n. 11
0
import torchvision.transforms as transforms
import torchvision.datasets as datasets

# default whether choose cuda
#th.long = torch.cuda.LongTensor 
#th.double = torch.cuda.DoubleTensor 
#th.int = torch.cuda.IntTensor 
#th.short = torch.cuda.ShortTensor 
#th.byte = torch.cuda.ByteTensor 
#th.half = torch.cuda.HalfTensor 
#th.char = torch.cuda.CharTensor 
#th.float = torch.cuda.FloatTensor 

# add summary to torch.nn.Module
nn.Module.summary = lambda self, inputShape=None, group=None, ganNoise=False:summary(self, inputShape or getModelDefaultInputShape(self, group, ganNoise) ,device=['cuda', 'cpu']['cpu' in str(getpara(self).device)])


def dedp(model):
    '''get raw model instead of torch.nn.DataParallel '''
    return model.module if isinstance(model, torch.nn.DataParallel) else model

if 'Module' in str(torch.nn.Module.load_state_dict):
    torch.rawModule = rawModule = torch.nn.Module.load_state_dict
else :
    rawModule = torch.rawModule 
def tryLoad(self, state_dict, strict=True):
    try:
        rawModule(self, state_dict, strict)
    except (KeyError,RuntimeError) :
        print('\x1b[31m%s\x1b[0m' % '\n"try strict=False! in Module.load_state_dict() " messge from boxx.ylth \n')
Esempio n. 12
0
            loss = lossFunction(output,labels) # calculate loss
            loss.backward() # backpropagate
            optimizer.step() # update weights

            total_samples += labels.size(0)
            total_loss += loss.item()

            if idx % 100 == 0:
                print("Running loss:", total_loss)

    final_time = (time()-time0)/60 
    print("Model trained in ", final_time, "minutes on ", total_samples, "samples")


model = Net()
print(summary(Net().cuda(), input_size=(1, 28 ,28), batch_size=-1))
train(model, train_loader)


def test(model, test_loader):
    """Test neural net"""

    correct = 0
    total = 0 

    with torch.no_grad():
        for idx, (images, labels) in enumerate(test_loader):
            images = images.view(images.shape[0],-1) # flatten
            output = model(images)
            values, indices = torch.max(output.data, 1)
            total += labels.size(0)
Esempio n. 13
0
    def show_model_summary(self):

        summary(self.model, (3, 32, 32))
Esempio n. 14
0
from mod_decomposer import *
from models.custom_models import *
from torchsummary import summary

device = 'cuda'
net = torch.load('prova.pth')
net = net.cpu()

layer = net.conv2
layer_cmp = pytorch_tucker_layer_decomposition(layer)
print(summary(net.to(device), (3, 32, 32)))

net.conv2 = layer_cmp
print(summary(net.to(device), (3, 32, 32)))

torch.save(net, 'decomposed.pth')
Esempio n. 15
0
        self.stage4 = nn.Sequential(
            DownSampling(160, 3, 2),
            NormalBlock(320, 3, 1),
            NormalBlock(320, 3, 1),
            NormalBlock(320, 3, 1),
        )

        self.embedding = Embedding(320, num_classes)

    def forward(self, x):
        x = self.conv1(x)
        x = self.head(x)
        x = self.stage2(x)
        x = self.stage3(x)
        x = self.stage4(x)

        out = self.embedding(x)
        return out


if __name__ == '__main__':
    model = VarGFaceNet()
    input = torch.randn(1, 3, 112, 112)
    out = model(input)
    print(out.shape)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = model.to(device)

    summary(model, (3, 112, 112))  # 必须开cuda,不需要传入batch_size
Esempio n. 16
0
def cifar10_testloader(root='./data', batch_size=32, num_workers=0):
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    testset = datasets.CIFAR10(root, train=False, download=True, transform=transform)
    return data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=num_workers)

input_size = (3, 32, 32)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0

#model = Keras_Cifar2(20, 5)
model = Keras_Cifar_AllConv()
model = LenetZhang()

if len(sys.argv) == 3:
    print("Loading model from " + str(sys.argv[2]))
    model = torch.load(sys.argv[2])
model_path = sys.argv[1]
model = torch.load(model_path)
model.cpu()
model.eval()

#print(torch_summarize(model))
print('Number of trainable params: ',sum([param.nelement() for param in model.parameters()]))
print(summary(model.to(device), input_size))

print("model loaded. Now testing...")
test_model_cifar10(cifar10_testloader(), model)
Esempio n. 17
0
def resnet34(norm_layer='bn', **kwargs):
    model = ResNet(BasicBlock, [3, 4, 6, 3], norm_layer=norm_layer, **kwargs)
    return model


def resnet50(norm_layer='bn', **kwargs):
    model = ResNet(Bottleneck, [3, 4, 6, 3], norm_layer=norm_layer, **kwargs)
    return model


def resnet101(norm_layer='bn', **kwargs):
    model = ResNet(Bottleneck, [3, 4, 23, 3], norm_layer=norm_layer, **kwargs)
    return model


def resnet152(norm_layer='bn', **kwargs):
    model = ResNet(Bottleneck, [3, 8, 36, 3], norm_layer=norm_layer, **kwargs)
    return model


def resnet200(norm_layer=None, **kwargs):
    model = ResNet(Bottleneck, [3, 24, 36, 3], norm_layer=norm_layer, **kwargs)
    return model


if __name__ == '__main__':
    from torchsummary import summary

    model = resnet101(norm_layer='frn')
    summary(model.cuda(), (3, 256, 256))
Esempio n. 18
0
if __name__ == "__main__":
    in_channel_1 = 32
    in_channel_2 = 64
    in_channel_thin = 1
    out_channel_thin = 32
    model = CombNet(in_channel_1=in_channel_1,
                    in_channel_2=in_channel_2,
                    in_channel_thin=in_channel_thin,
                    out_channel_thin=out_channel_thin,
                    num_class=14)
    input_1_shape = [in_channel_1, 512, 512, 8 * 4]
    input_2_shape = [in_channel_2, 128, 256, 256, 8 * 2]
    input_thin_shape = [in_channel_thin, 128, 128, 8]

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    model.to(device)
    from torchsummary import summary
    summary(model, [input_1_shape, input_2_shape, input_thin_shape])

    input_1_dummy = torch.rand(input_1_shape).to(device)
    input_2_dummy = torch.rand(input_2_shape).to(device)
    input_thin_dummy = torch.rand(input_thin_shape).to(device)

    print("Device:", device)
    print("Input:", input_1_dummy.shape, input_2_dummy.shape,
          input_thin_dummy.shape)
    #output = model(input_1_dummy, input_2_dummy, input_thin_dummy)

    print("Output :", output.size())
Esempio n. 19
0
        in_channels = 3
        for x in cfg:
            if x == 'M':
                layers += [nn.MaxPool2d(2, 2)]
            else:
                conv2d = nn.Conv2d(in_channels=in_channels,
                                   out_channels=x,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1)
                # batch_norm이 true
                if batch_norm:
                    layers += [
                        conv2d,
                        nn.BatchNorm2d(x),
                        nn.ReLU(inplace=True)
                    ]  # inplace 메모리 감소 -> 따로 찾아볼것
                else:
                    layers += [conv2d, nn.ReLU(inplace=True)]
                in_channels = x

        return nn.Sequential(*layers)


if __name__ == "__main__":
    from torchsummary import summary

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net = VGGNet('VGG16').to(device)
    summary(net, (3, 224, 224), 30)
Esempio n. 20
0
                        nn.ReLU(inplace=True)
                    ]
                elif Ltype == 'N':
                    layers += [
                        nn.Conv2d(self.in_channels,
                                  F,
                                  kernel_size=K,
                                  padding=K // 2),
                        nn.BatchNorm2d(F),
                        nn.ReLU(inplace=True)
                    ]
                self.in_channels = F


net = NET(netType).to(device)
summary(net, (1, 3, 32, 32))
#flops, params = profile(net, input_size=(1, 3, 32,32), custom_ops={xCNN: count_op_xCNN, xCNNlow: count_op_xCNNlow})
#print(flops,params)

criterion = nn.CrossEntropyLoss()

opt = optim.Adam(net.parameters(), lr=lrate)

if os.path.isfile(load_path) and load_ckpt:
    checkpoint = torch.load(load_path)
    net.load_state_dict(checkpoint['model_state_dict'])
    opt.load_state_dict(checkpoint['opt'])
    start_epoch = checkpoint['epoch'] + 1
    print('model loaded')

for epoch in range(start_epoch,
Esempio n. 21
0
def main(args):
    train_dataset = UrbanSound8KDataset('UrbanSound8K_train.pkl', "LMC")

    training_class_counts = np.array(
        [6295, 1825, 6248, 5121, 5682, 6282, 1112, 5886, 5819, 6299])
    class_weights = 1 / training_class_counts
    data_weights = []

    for i in range(0, len(train_dataset)):
        features, label, filename, labelname = train_dataset.__getitem__(i)
        data_weight = class_weights[label]
        data_weights.append(data_weight)

    sampler = torch.utils.data.sampler.WeightedRandomSampler(
        torch.tensor(data_weights), len(train_dataset))

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        shuffle=False,
        batch_size=args.batch_size,
        pin_memory=True,
        num_workers=args.worker_count,
        sampler=sampler,
    )

    test_loader = torch.utils.data.DataLoader(
        UrbanSound8KDataset('UrbanSound8K_test.pkl', "LMC"),
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )

    model = LMCNet(height=85,
                   width=41,
                   channels=1,
                   class_count=10,
                   dropout=args.dropout)

    ## TASK 8: Redefine the criterion to be softmax cross entropy
    criterion = nn.CrossEntropyLoss()

    ## TASK 11: Define the optimizer
    optimizer = optim.Adam(model.parameters(),
                           lr=args.learning_rate,
                           betas=(0.9, 0.999),
                           weight_decay=0.004)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)
    trainer = Trainer(model, train_loader, test_loader, criterion, optimizer,
                      summary_writer, DEVICE)

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary(model, input_size=(1, 85, 41))

    summary_writer.close()
def train():
    if args.dataset == 'CARPLATE_ONLY_FOUR_CORNERS_WITH_BORDER':
        cfg = carplate
        if args.input_size == 512:
            cfg = change_cfg_for_ssd512(cfg)
        dataset = CARPLATE_FOUR_CORNERSDetection(
            root=args.dataset_root,
            transform=SSDAugmentation_four_corners(cfg['min_dim'], MEANS),
            dataset_name='trainval')
        from data import CARPLATE_FOUR_CORNERS_CLASSES as labelmap
        eval_dataset = CARPLATE_FOUR_CORNERSDetection(
            root=args.dataset_root,
            transform=BaseTransform(args.input_size, MEANS),
            target_transform=CARPLATE_FOUR_CORNERSAnnotationTransform(
                keep_difficult=True),
            dataset_name='test')

    if args.visdom:
        import visdom
        global viz
        viz = visdom.Visdom()

    ssd_net = build_ssd('train', cfg['min_dim'], cfg['num_classes'])
    net = ssd_net

    # summary
    summary(net, input_size=(3, int(cfg['min_dim']), int(cfg['min_dim'])))

    if args.cuda:
        net = torch.nn.DataParallel(ssd_net)
        cudnn.benchmark = True

    if args.resume:
        print('Resuming training, loading {}...'.format(args.resume))
        ssd_net.load_weights(args.resume)
    else:
        vgg_weights = torch.load('weights/' + args.basenet)
        print('Loading base network...')
        ssd_net.vgg.load_state_dict(vgg_weights)

    if args.cuda:
        net = net.cuda()

    if not args.resume:
        print('Initializing weights...')
        # initialize newly added layers' weights with xavier method
        ssd_net.extras.apply(weights_init)
        ssd_net.conf.apply(weights_init)
        ssd_net.four_corners.apply(weights_init)

    optimizer = optim.SGD(net.parameters(),
                          lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay)
    # optimizer = optim.Adam(net.parameters(), lr=args.lr, betas=(0.9, 0.999),
    #                        weight_decay=args.weight_decay)
    criterion = MultiBoxLoss_only_four_corners_with_CIoU(
        cfg['num_classes'], 0.5, True, 0, True, 3, 0.5, False, args.cuda)

    net.train()
    # loss counters
    conf_loss = 0
    four_corners_loss = 0
    border_loss = 0
    epoch = 0
    print('Loading the dataset...')

    epoch_size = len(dataset) // args.batch_size
    print('Training SSD on:', dataset.name)
    print('Using the specified args:')
    print(args)

    step_index = 0

    if args.visdom:
        vis_title = 'SSD.PyTorch on ' + dataset.name
        vis_legend = [
            'Conf Loss', 'Four Corners Loss', 'Border Loss', 'Total Loss'
        ]
        iter_plot = create_vis_plot('Iteration', 'Loss', vis_title, vis_legend)
        epoch_plot = create_vis_plot('Epoch', 'Loss', vis_title, vis_legend)

    data_loader = data.DataLoader(dataset,
                                  args.batch_size,
                                  num_workers=args.num_workers,
                                  shuffle=True,
                                  collate_fn=detection_collate,
                                  pin_memory=True)

    lr = args.lr
    # create batch iterator
    batch_iterator = iter(data_loader)
    for iteration in range(args.start_iter, cfg['max_iter']):
        if args.visdom and iteration != 0 and (iteration % epoch_size == 0):
            epoch += 1
            update_vis_plot(epoch, conf_loss, four_corners_loss, border_loss,
                            epoch_plot, None, 'append', epoch_size)
            # reset epoch loss counters
            conf_loss = 0
            four_corners_loss = 0
            border_loss = 0

        if iteration in cfg['lr_steps']:
            step_index += 1
            lr = adjust_learning_rate(optimizer, args.gamma, epoch, step_index,
                                      iteration, epoch_size)

        # load train data
        try:
            images, targets = next(batch_iterator)
        except StopIteration:
            batch_iterator = iter(data_loader)
            images, targets = next(batch_iterator)

        if args.cuda:
            images = Variable(images.cuda())
            with torch.no_grad():
                targets = [Variable(ann.cuda()) for ann in targets]
        else:
            images = Variable(images)
            with torch.no_grad():
                targets = [Variable(ann) for ann in targets]
        # forward
        t0 = time.time()
        out = net(images)
        # backprop
        optimizer.zero_grad()
        loss_c, loss_four_corners, loss_border = criterion(out, targets)
        loss = loss_c + loss_four_corners + loss_border
        loss.backward()
        optimizer.step()
        t1 = time.time()
        conf_loss += loss_c.item()
        four_corners_loss += loss_four_corners.item()
        border_loss += loss_border.item()

        if iteration % 100 == 0:
            log.l.info('''
                Timer: {:.5f} sec.\t LR: {}.\t Iter: {}.\t Loss_c: {:.5f}.\t Loss_four_corners: {:.5f}.\t Loss_border: {:.5f}.\t Loss: {:.5f}.
                '''.format(
                (t1 - t0),
                lr,
                iteration,
                loss_c.item(),
                loss_four_corners.item(),
                loss_border.item(),
                loss_c.item() + loss_four_corners.item() + loss_border.item(),
            ))

        if args.visdom:
            update_vis_plot(iteration, loss_c.item(), loss_four_corners.item(),
                            loss_border.item(), iter_plot, epoch_plot,
                            'append')

        if iteration != 0 and iteration % 5000 == 0:
            print('Saving state, iter:', iteration)
            torch.save(
                ssd_net.state_dict(), 'weights/' + args.save_folder + 'ssd' +
                str(args.input_size) + '_' + repr(iteration) + '.pth')

            # load net for evaluation
            num_classes = len(labelmap) + 1  # +1 for background
            eval_net = build_ssd('test', args.input_size,
                                 num_classes)  # initialize SSD
            eval_net.load_state_dict(
                torch.load('weights/' + args.save_folder + 'ssd' +
                           str(args.input_size) + '_' + repr(iteration) +
                           '.pth'))
            eval_net.eval()
            print('Finished loading model!')
            if args.cuda:
                eval_net = eval_net.cuda()
                cudnn.benchmark = True
            # evaluation begin
            eval_results.test_net(args.eval_save_folder,
                                  args.obj_type,
                                  args.dataset_root,
                                  'test',
                                  labelmap,
                                  eval_net,
                                  args.cuda,
                                  eval_dataset,
                                  BaseTransform(eval_net.size, MEANS),
                                  args.top_k,
                                  args.input_size,
                                  thresh=args.confidence_threshold)

    torch.save(
        ssd_net.state_dict(), 'weights/' + args.save_folder + '' +
        args.dataset + str(args.input_size) + '.pth')
    # load net for evaluation for the final model
    num_classes = len(labelmap) + 1  # +1 for background
    eval_net = build_ssd('test', args.input_size,
                         num_classes)  # initialize SSD
    eval_net.load_state_dict(
        torch.load('weights/' + args.save_folder + '' + args.dataset +
                   str(args.input_size) + '.pth'))
    eval_net.eval()
    print('Finished loading model!')
    if args.cuda:
        eval_net = eval_net.cuda()
        cudnn.benchmark = True
    # evaluation begin
    eval_results.test_net(args.eval_save_folder,
                          args.obj_type,
                          args.dataset_root,
                          'test',
                          labelmap,
                          eval_net,
                          args.cuda,
                          eval_dataset,
                          BaseTransform(eval_net.size, MEANS),
                          args.top_k,
                          args.input_size,
                          thresh=args.confidence_threshold)
Esempio n. 23
0
        x_wc3 = self.wc3(x_res2)
        x_wc4 = self.wc4(x_res3)
        x = self.wc5(x)
        x = self.dec4(x, x_wc4)
        x = self.dec3(x, x_wc3)
        x = self.score(x)
        # with torch.no_grad():
        x = self.score_u8(x)

        return x


if __name__ == "__main__":
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = DF1SegX22(classes=19).to(device)
    summary(model, (3, 352, 480))
    x = torch.randn(2, 3, 512, 1024).to(device)

    from fvcore.nn.jit_handles import batchnorm_flop_jit
    from fvcore.nn.jit_handles import generic_activation_jit

    supported_ops = {
        "aten::batch_norm": batchnorm_flop_jit,
    }
    flop_dict, _ = flop_count(model, (x,), supported_ops)

    flops_count, params_count = get_model_complexity_info(model, (3, 512, 1024),
                                                          as_strings=False,
                                                          print_per_layer_stat=True)
    input = x
    macs, params = profile(model, inputs=(input,))
Esempio n. 24
0
def main():
    """
    Training and validation.
    """

    global best_bleu4, epochs_since_improvement, checkpoint, start_epoch, fine_tune_encoder, data_name, word_map

    # Read word map
    word_map_file = os.path.join(data_folder, 'WORDMAP_' + data_name + '.json')
    with open(word_map_file, 'r') as j:
        word_map = json.load(j)

    # Initialize / load checkpoint
    if checkpoint is None:
        decoder = DecoderRNN_SEN(emb_dim, decoder_dim, len(word_map), number_layers)
        # print(len(word_map))
        decoder_optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, decoder.parameters()),
                                             lr=decoder_lr)
        encoder = EncoderCNN_SEN(emb_dim)
        encoder_optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, encoder.parameters()),
                                             lr=encoder_lr) if fine_tune_encoder else None

    else:
        checkpoint = torch.load(checkpoint)
        start_epoch = checkpoint['epoch'] + 1
        epochs_since_improvement = checkpoint['epochs_since_improvement']
        best_bleu4 = checkpoint['bleu-4']
        decoder = checkpoint['decoder']
        decoder_optimizer = checkpoint['decoder_optimizer']
        encoder = checkpoint['encoder']
        encoder_optimizer = checkpoint['encoder_optimizer']
        if fine_tune_encoder is True and encoder_optimizer is None:
            encoder.fine_tune(fine_tune_encoder)
            encoder_optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, encoder.parameters()),
                                                 lr=encoder_lr)

    # Move to GPU, if available
    decoder = decoder.to(device)
    encoder = encoder.to(device)

    # Loss function
    criterion = nn.CrossEntropyLoss().to(device)

    # Custom dataloaders
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    '''
    去掉num_workers 不使用多线程解决报错 TypeError: h5py objects cannot be pickled
    train_loader = torch.utils.data.DataLoader(
        CaptionDataset_tra(data_folder, data_name, 'TRAIN', transform=transforms.Compose([normalize])),
        batch_size=batch_size, shuffle=True, num_workers=workers, pin_memory=True)
    '''
    train_loader = torch.utils.data.DataLoader(
        CaptionDataset_tra(data_folder, data_name, 'TRAIN', transform=transforms.Compose([normalize])),
        batch_size=batch_size, shuffle=True, pin_memory=True, drop_last=True)  # set drop_last=True to solve the error:

    val_loader = torch.utils.data.DataLoader(
        CaptionDataset_tra(data_folder, data_name, 'VAL', transform=transforms.Compose([normalize])),
        batch_size=32, shuffle=True, pin_memory=True, drop_last=True)

    # torchsummary
    summary(encoder, input_size=(3, 224, 224))

    # Epochs
    for epoch in range(start_epoch, epochs):

        # Decay learning rate if there is no improvement for 8 consecutive epochs, and terminate training after 20
        if epochs_since_improvement == 10:
            break
        if epochs_since_improvement > 0 and epochs_since_improvement % 8 == 0:
            adjust_learning_rate(decoder_optimizer, 0.8)
            if fine_tune_encoder:
                adjust_learning_rate(encoder_optimizer, 0.8)

        # One epoch's training
        train(train_loader=train_loader,
              encoder=encoder,
              decoder=decoder,
              criterion=criterion,
              encoder_optimizer=encoder_optimizer,
              decoder_optimizer=decoder_optimizer,
              epoch=epoch)
        # One epoch's validation
        recent_bleu4 = validate(val_loader=val_loader,
                                encoder=encoder,
                                decoder=decoder,
                                criterion=criterion)

        # Check if there was an improvement
        is_best = recent_bleu4 > best_bleu4
        best_bleu4 = max(recent_bleu4, best_bleu4)
        if not is_best:
            epochs_since_improvement += 1
            print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement,))
        else:
            epochs_since_improvement = 0

        # Save checkpoint
        save_checkpoint_tra(data_name, epoch, epochs_since_improvement, encoder, decoder, encoder_optimizer,
                            decoder_optimizer, recent_bleu4,
                            is_best)  # add 'checkpoint' to save the model and parameters By zjx
Esempio n. 25
0
def disp_summary(model):
    #use_cuda= torch.cuda.is_available()
    #device=torch.device('cuda' if use_cuda else 'cpu')
    ##model=Net().to(device)
    summary(model, input_size=(3, 32, 32))
Esempio n. 26
0
                                    nn.ReLU(True))
        self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2),
                                    nn.ReLU(True))
        self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, out_dim),
                                    nn.ReLU(True))

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        return x


model = neural_network(28 * 28, 300, 100, 10)
if use_gpu: model = model.cuda()
summary(model, (1, 28 * 28))

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# start training
for epoch in range(1, num_epochs + 1):
    print('*' * 20)
    print(f'epoch {epoch}')
    sum_loss = .0
    sum_acc = .0
    model.train()
    for i, data in enumerate(train_loader, 1):
        img, label = data
        img = img.view(img.size(0), -1)
        if use_gpu:
def __extract_features_rgb(begin_num=None, end_num=None):
    root_path = c.DATA_ROOT_PATH
    annotation_path = '%s/Charades/annotation/frames_dict_trimmed_multi_label_i3d_160_frames.pkl' % (
        root_path)
    features_root_path = '%s/Charades/features_i3d_charades_rgb_mixed_5c_trimmed_20_frames' % (
        root_path)
    video_frames_root_path = '%s/Charades/frames/Charades_v1_rgb' % (root_path)
    model_path = '%s/Charades/baseline_models/i3d/rgb_charades.pt' % (
        root_path)
    feature_name = 'Mixed_5c'

    (video_frames_dict_tr,
     video_frames_dict_te) = utils.pkl_load(annotation_path)
    video_frames_dict = dict()
    video_frames_dict.update(video_frames_dict_tr)
    video_frames_dict.update(video_frames_dict_te)
    video_names = video_frames_dict.keys()

    n_videos = len(video_names)
    frame_count = 0

    if not os.path.exists(features_root_path):
        print('Sorry, path does not exist: %s' % (features_root_path))
        return

    t1 = time.time()
    print('extracting training features')
    print('start time: %s' % utils.timestamp())

    # aync reader, and get load images for the first video
    img_reader = image_utils.AsyncImageReaderCharadesForI3DTorchModel(
        n_threads=20)
    img_reader.load_imgs_in_batch(
        __get_video_frame_pathes(video_names[0], video_frames_root_path,
                                 video_frames_dict))

    # load the model
    model = __load_i3d_model_rgb(model_path)
    torchsummary.summary(model, input_size=(3, 160, 224, 224))

    # loop on list of videos
    for idx_video in range(n_videos):
        video_num = idx_video + 1

        if begin_num is not None and end_num is not None:
            if video_num <= begin_num or video_num > end_num:
                continue

        video_name = video_names[idx_video]

        # wait untill the image_batch is loaded
        t1 = time.time()
        while img_reader.is_busy():
            threading._sleep(0.1)
        t2 = time.time()
        duration_waited = t2 - t1
        print('...... video %d/%d: %s, waited: %d' %
              (video_num, n_videos, video_name, duration_waited))

        # get the video frames
        video_frames = img_reader.get_images()

        # pre-load for the next video
        if video_num < n_videos:
            next_video_name = video_names[idx_video + 1]
            img_reader.load_imgs_in_batch(
                __get_video_frame_pathes(next_video_name,
                                         video_frames_root_path,
                                         video_frames_dict))

        video_features_path = '%s/%s.pkl' % (features_root_path, video_name)
        # if os.path.exists(video_features_path):
        #     print ('... features for video already exist: %s.pkl' % (video_name))
        #     continue

        if len(video_frames) != 160:
            print('... wrong n frames: %d' % (video_num))
            continue

        # transpose to have the channel_first (160, 224, 224, 3) => (3, 160, 224, 224)
        video_frames = np.transpose(video_frames, (3, 0, 1, 2))

        # add one dimension to represent the batch size
        video_frames = np.expand_dims(video_frames, axis=0)

        # prepare input variable
        with torch.no_grad():
            # extract features
            input_var = torch.from_numpy(video_frames).cuda()
            output_var = model(input_var)
            output_var = output_var.cpu()
            features = output_var.data.numpy()  # (1, 1024, 20, 7, 7)

            # don't forget to clean up variables
            del input_var
            del output_var

        # squeeze to remove the dimension of the batch_size
        features = features[0]  # (1024, 20, 7, 7)

        # transpose to have the channel_last
        features = np.transpose(features, (1, 2, 3, 0))  # (20, 7, 7, 1024)

        # path to save the features
        utils.pkl_dump(features, video_features_path, is_highest=True)

        # increment counts
        frame_count += len(video_frames)

    t2 = time.time()
    print('finish extracting %d features in %d seconds' %
          (frame_count, t2 - t1))
    print('end time: %s' % utils.timestamp())
Esempio n. 28
0
 def print_summary(self):
     print('Model Summary')
     summary(self, input_size=(1, 40, 801))
     print('\n')
Esempio n. 29
0
            label = label.to(device=device, dtype=torch.float32)
            # image, label = Variable(image, requires_grad=False), Variable(label, requires_grad=False)
            
            # image, label = Variable(image.cuda(), requires_grad=False), Variable(label.cuda(),
            #                                                                                 requires_grad=False)
            # 使用网络参数,输出预测结果
            pred = net(image)
            # 计算loss
            loss = criterion(pred, label)
            print('Loss/train', loss.item())
            # 保存loss值最小的网络参数
            if loss < best_loss:
                best_loss = loss
                torch.save(net.state_dict(), 'best_model.pth')
            # 更新参数
            loss.backward()
            optimizer.step()

if __name__ == "__main__":
    # 选择设备,有cuda用cuda,没有就用cpu
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    # 加载网络,图片单通道1,分类为1。
    net = UNet(n_channels=1, n_classes=1)
    
    # 将网络拷贝到deivce中
    net.to(device=device)

    summary(net, (1, 512, 512))
    # 指定训练集地址,开始训练
    data_path = r"data/train/"
    train_net(net, device, data_path)
Esempio n. 30
0
                nn.InstanceNorm2d(conv_dim * 2 ** i, affine=True, track_running_stats=True),
                nn.LeakyReLU(negative_slope=0.2, inplace=True)
            ))
            in_channels = conv_dim * 2 ** i
        self.conv = nn.Sequential(*layers)
        feature_size = image_size // 2**n_layers
        self.fc_adv = nn.Sequential(
            nn.Linear(conv_dim * 2 ** (n_layers - 1) * feature_size ** 2, fc_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
            nn.Linear(fc_dim, 1)
        )
        self.fc_att = nn.Sequential(
            nn.Linear(conv_dim * 2 ** (n_layers - 1) * feature_size ** 2, fc_dim),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
            nn.Linear(fc_dim, attr_dim),
        )

    def forward(self, x):
        y = self.conv(x)
        y = y.view(y.size()[0], -1)
        logit_adv = self.fc_adv(y)
        logit_att = self.fc_att(y)
        return logit_adv, logit_att


if __name__ == '__main__':
    gen = Generator(5, n_layers=6, shortcut_layers=5, use_stu=True, one_more_conv=True)
    summary(gen, [(3, 384, 384), (5,)], device='cpu')

    dis = Discriminator(image_size=384, attr_dim=5)
    summary(dis, (3, 384, 384), device='cpu')
Esempio n. 31
0
def _test():
    from torchsummary import summary
    model = DenseNet264()
    model = model.cuda()
    summary(model,input_size=(3,224,224))
Esempio n. 32
0
def train(args):
    # initialize visdomX
    vis = VisdomX()
    
    # prepare the MNIST dataset
    train_dataset = datasets.MNIST(root="./data/",
                                   train=True, 
                                   transform=transforms.ToTensor(),
                                   download=True)

    test_dataset = datasets.MNIST(root="./data/",
                                  train=False, 
                                  transform=transforms.ToTensor())

    # create the data loader
    train_loader = DataLoader(dataset=train_dataset,
                              batch_size=args.batch_size, 
                              shuffle=True, drop_last=True)

    test_loader = DataLoader(dataset=test_dataset,
                             batch_size=args.batch_size, 
                             shuffle=False)

    
    # turn on the CUDA if available
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
    net = Net().to(device)
    loss_op = nn.CrossEntropyLoss()
    optim   = torch.optim.Adam(net.parameters(), lr=args.lr)
    
    # model summary via torchsummary
    summary(net, (1, 28, 28))

    for epoch in range(args.max_epochs):
        net.train()
        for step, inputs in enumerate(train_loader):
            images = inputs[0].to(device)
            labels = inputs[1].to(device)
            
            # forward-propagation
            outputs = net(images)
            loss = loss_op(outputs, labels)
            
            # back-propagation
            optim.zero_grad()
            loss.backward()
            optim.step()

        acc = evaluate(net, test_loader, device)

        # plot loss and acc by visdom
        vis.add_scalars(loss.item(), epoch,
                        title="Loss",
                        ylabel="Loss", xlabel="Epoch")
        vis.add_scalars(acc, epoch,
                        title="Accuracy",
                        ylabel="Accuracy", xlabel="Epoch")
        
        print("Epoch [{}/{}] loss: {:.5f} test acc: {:.3f}"
              .format(epoch+1, args.max_epochs, loss.item(), acc))

    torch.save(net.state_dict(), "mnist-final.pth")
Esempio n. 33
0
validate_loader = torch.utils.data.DataLoader(dataset=validate_imgs,
                                              pin_memory=use_cuda,
                                              shuffle=True,
                                              batch_size=batch_size)
conf = dict()
conf['noise_level'] = noise_level
conf['lr'] = lr
conf['kl_factor'] = kl_factor
conf['norm_mean'] = mean
conf['norm_std'] = std
vae = VAE(n_channel=3,
          img_size=img_size,
          z_dim=z_dim,
          use_cuda=use_cuda,
          conf=conf)
summary(vae, input_size=(3, 64, 64), batch_size=batch_size)
tbar = tqdm(range(num_epochs))
fid = open('losses', 'w')
for epoch in tbar:
    train_rec, train_kl = vae.one_epoch(train_loader)
    valid_rec, valid_kl = vae.evaluate(validate_loader)

    vae.eval()
    bx_train, _ = iter(train_loader).next()
    bx_valid, _ = iter(validate_loader).next()
    bx_train = bx_train[:8]
    bx_valid = bx_valid[:8]
    rand_samp = vae.random_sample(num_samples=8)
    rec_train = vae.reconstruct_img(bx_train)
    rec_valid = vae.reconstruct_img(bx_valid)
    bx_train = vae.unnormalize(bx_train)
Esempio n. 34
0
# net = CPD_All_Conv(int(args.ranks[0]), int(args.ranks[1]), int(args.ranks[2]), relu=False)
# net = Keras_Cifar_classic()
net = LenetZhang()
# net = CPD_Zhang(int(args.ranks[0]), int(args.ranks[1]), int(args.ranks[2]), relu=False)

# GPU
if args.model:
    net = torch.load(args.model)
if args.state:
    net.load_state_dict(torch.load(args.state))

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = net.to(device)

print('###################')
print(summary(net, (3, 32, 32)))
print('Number of trainable params:',
      sum([param.nelement() for param in net.parameters()]))

########################################################################
# 3. Define a Loss function and optimizer
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Let's use a Classification Cross-Entropy loss and SGD with momentum

import torch.optim as optim

criterion = nn.CrossEntropyLoss()

if args.opt == 'Adam':
    optimizer = optim.Adam(net.parameters(), lr=0.001)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.1)
Esempio n. 35
0
 def summary(self, shape):
     return summary(self, shape, device='cpu')