コード例 #1
0
    def __init__(self, num_classes=1):
        super(DinkNet34_more_dilate, self).__init__()

        filters = [64, 128, 256, 512]
        resnet = models.resnet34(pretrained=True)
        
        self.firstconv = resnet.conv1
        self.firstbn = resnet.bn1
        self.firstrelu = resnet.relu
        self.firstmaxpool = resnet.maxpool
        self.encoder1 = resnet.layer1
        self.encoder2 = resnet.layer2
        self.encoder3 = resnet.layer3
        
        self.dblock = Dblock_more_dilate(256)

        self.decoder3 = DecoderBlock(filters[2], filters[1])
        self.decoder2 = DecoderBlock(filters[1], filters[0])
        self.decoder1 = DecoderBlock(filters[0], filters[0])

        self.finaldeconv1 = nn.ConvTranspose2d(filters[0], 32, 4, 2, 1)
        self.finalrelu1 = nonlinearity
        self.finalconv2 = nn.Conv2d(32, 32, 3, padding=1)
        self.finalrelu2 = nonlinearity
        self.finalconv3 = nn.Conv2d(32, num_classes, 3, padding=1)
コード例 #2
0
def get_features(imgs, batch_size=100):
    resnet = models.resnet34(pretrained=True).cuda(device_id).eval()
    resnet_feature = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu,
                                   resnet.maxpool, resnet.layer1,
                                   resnet.layer2, resnet.layer3, resnet.layer4).cuda(device_id).eval()
    trans = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Scale(64),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])

    feature_conv, feature_smax, feature_class = [], [], []
    for batch in tqdm(imgs.split(batch_size)):
        batch = torch.stack(list(map(trans, batch)), 0)
        input = Variable(batch.cuda(device_id))
        fconv = resnet_feature(input)
        fconv = fconv.mean(3).mean(2).squeeze()
        flogit = resnet.fc(fconv)
        fsmax = F.softmax(flogit)
        feature_conv.append(fconv.data.cpu())
        feature_class.append(flogit.data.cpu())
        feature_smax.append(fsmax.data.cpu())
    feature_conv = torch.cat(feature_conv, 0)
    feature_class = torch.cat(feature_class, 0)
    feature_smax = torch.cat(feature_smax, 0)
    return feature_conv, feature_class, feature_smax
コード例 #3
0
def resnet34(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-34 model.
    """
    model = models.resnet34(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet34'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    return model
コード例 #4
0
ファイル: network.py プロジェクト: jimchenhub/Xlearn
 def __init__(self):
   super(ResNet34Fc, self).__init__()
   model_resnet34 = models.resnet34(pretrained=True)
   self.conv1 = model_resnet34.conv1
   self.bn1 = model_resnet34.bn1
   self.relu = model_resnet34.relu
   self.maxpool = model_resnet34.maxpool
   self.layer1 = model_resnet34.layer1
   self.layer2 = model_resnet34.layer2
   self.layer3 = model_resnet34.layer3
   self.layer4 = model_resnet34.layer4
   self.avgpool = model_resnet34.avgpool
   self.__in_features = model_resnet34.fc.in_features
コード例 #5
0
    def __init__(self, requires_grad=False, pretrained=True, num=18):
        super(resnet, self).__init__()
        if(num==18):
            self.net = models.resnet18(pretrained=pretrained)
        elif(num==34):
            self.net = models.resnet34(pretrained=pretrained)
        elif(num==50):
            self.net = models.resnet50(pretrained=pretrained)
        elif(num==101):
            self.net = models.resnet101(pretrained=pretrained)
        elif(num==152):
            self.net = models.resnet152(pretrained=pretrained)
        self.N_slices = 5

        self.conv1 = self.net.conv1
        self.bn1 = self.net.bn1
        self.relu = self.net.relu
        self.maxpool = self.net.maxpool
        self.layer1 = self.net.layer1
        self.layer2 = self.net.layer2
        self.layer3 = self.net.layer3
        self.layer4 = self.net.layer4
コード例 #6
0
ファイル: learner.py プロジェクト: AragornThorongil/DGS
                                             download=False,
                                             transform=test_transform)
    elif args.model == 'ResNet18OnCifar10':
        model = ResNetOnCifar10.ResNet18()

        train_transform, test_transform = get_data_transform('cifar')
        train_dataset = datasets.CIFAR10(args.data_dir,
                                         train=True,
                                         download=False,
                                         transform=train_transform)
        test_dataset = datasets.CIFAR10(args.data_dir,
                                        train=False,
                                        download=False,
                                        transform=test_transform)
    elif args.model == 'ResNet34':
        model = models.resnet34(pretrained=False)

        train_transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        test_transform = train_transform
        train_dataset = datasets.ImageFolder(args.data_dir,
                                             train=True,
                                             download=False,
                                             transform=train_transform)
        test_dataset = datasets.ImageFolder(args.data_dir,
                                            train=False,
                                            download=False,
                                            transform=test_transform)
コード例 #7
0
    val_dataset = CovidDataset(img_folder,
                               train=False,
                               transform=val_transforms,
                               train_size=parameters['train_size'])
    val_loader = DataLoader(dataset=val_dataset,
                            shuffle=True,
                            num_workers=4,
                            batch_size=11)
    print("Train samples: {}\nValidation samples: {}\nTotal samples: {}\n".
          format(len(train_dataset), len(val_dataset),
                 len(train_dataset) + len(val_dataset)))
    dataloaders = {'train': train_loader, 'val': val_loader}

    if not os.path.isfile("./net_with_bce.pth"):
        model_ft = models.resnet34(pretrained=True)
        num_ftrs = model_ft.fc.in_features
        # Here the size of each output sample is set to 2.
        model_ft.fc = nn.Linear(num_ftrs, 2)
        model_ft = model_ft.to(device)
        """
        The parameter pos_weight:
        * >1 -> increase recall
        * <1 -> increase precision
        """
        criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(5.))

        # Observe that all parameters are being optimized
        optimizer_ft = optim.SGD(model_ft.parameters(),
                                 lr=parameters['lr'],
                                 momentum=0.9,
コード例 #8
0
ファイル: model.py プロジェクト: kratika-lahoti/GradingKneeOA
def cls_model(args):
    if args.net_type == "resnet":
        if args.depth == "18":
            model = models.resnet18(pretrained=args.pretrained)
        elif args.depth == "34":
            model = models.resnet34(pretrained=args.pretrained)
        elif args.depth == "50":
            model = models.resnet50(pretrained=args.pretrained)
        elif args.depth == "101":
            model = models.resnet101(pretrained=args.pretrained)
        elif args.depth == "152":
            model = models.resnet152(pretrained=args.pretrained)
        else:
            return None

        in_features = model.fc.in_features
        model.fc = torch.nn.Linear(in_features, args.num_class)

    elif args.net_type == "vgg":
        if args.depth == "16":
            model = models.vgg16(pretrained=args.pretrained)
        elif args.depth == "19":
            model = models.vgg19(pretrained=args.pretrained)
        elif args.depth == "16bn":
            model = models.vgg16_bn(pretrained=args.pretrained)
        elif args.depth == "19bn":
            model = models.vgg19_bn(pretrained=args.pretrained)
        else:
            return None

        num_ftrs = model.classifier[6].in_features
        feature_model = list(model.classifier.children())
        feature_model.pop()
        feature_model.append(nn.Linear(num_ftrs, args.num_class))
        model.classifier = nn.Sequential(*feature_model)

    elif args.net_type == "densenet":
        if args.depth == "121":
            model = models.densenet121(pretrained=args.pretrained)
        elif args.depth == "169":
            model = models.densenet169(pretrained=args.pretrained)
        elif args.depth == "201":
            model = models.densenet201(pretrained=args.pretrained)
        else:
            return None

        in_features = model.classifier.in_features
        model.classifier = torch.nn.Linear(in_features, args.num_class)

    elif args.net_type == "inception":
        if args.depth == "v3":
            model = models.inception_v3(pretrained=args.pretrained)
        else:
            return None

        in_features = model.fc.in_features
        model.fc = torch.nn.Linear(in_features, args.num_class)

    else:
        return None

    return model
コード例 #9
0
def main():
    global args, best_acc

    args = parser.parse_args()

    # resnet-34, or could change the model for efficiency
    model = models.resnet34(True)
    model.fc = nn.Linear(model.fc.in_features, 2)  # for trible classification
    pre_state_dict = torch.load('./G_checkpoint_best.pth')['state_dict']
    # #pre_state_dict = torch.load('./checkpoints/LU_V3.pth')
    model.load_state_dict(pre_state_dict)
    model.cuda()

    device_ids = range(torch.cuda.device_count())
    print(device_ids)

    # if necessary, mult-gpu training
    if len(device_ids) > 1:
        model = torch.nn.DataParallel(model)

    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-4)

    cudnn.benchmark = True

    # normalization
    normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.1, 0.1, 0.1])
    trans = transforms.Compose([transforms.ToTensor(), normalize])

    # load data
    train_dset = myDataset(csv_path='./coords/G_TwoTypes_Train.csv',
                           transform=trans)
    train_loader = torch.utils.data.DataLoader(train_dset,
                                               batch_size=args.batch_size,
                                               shuffle=False,
                                               num_workers=args.workers,
                                               pin_memory=False)
    val_dset = myDataset(csv_path='./coords/G_TwoTypes_Test.csv',
                         transform=trans)
    val_loader = torch.utils.data.DataLoader(val_dset,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=False)

    # loop throuh epochs
    # for evaluation,
    train_dset.setmode(1)
    val_dset.setmode(1)
    # slideIDX --> Patch_level label
    # get all problities of all patches in the loader
    train_probs = inference(123, train_loader, model)
    print(train_probs.shape)
    val_probs = inference(123, val_loader, model)
    # choose most K probable patch per slide, k = 2
    train_topk = group_argtopk(np.array(train_dset.slideIDX), train_probs, 10)
    val_topk = group_argtopk(np.array(val_dset.slideIDX), val_probs, 10)
    print(len(train_dset.slideIDX), len(train_topk))
    print(len(val_dset.slideIDX), len(val_topk))

    data = {
        "train_patch_probs": train_probs,
        "val_patch_probs": val_probs,
        "train_top_k": train_topk,
        "val_top_k": val_topk
    }
    torch.save(data, './for_rnn_train.pth')
コード例 #10
0
    def __init__(self, norm_layer=nn.BatchNorm2d, **kwargs):
        super(Simple3DPoseBaseSMPL24, self).__init__()
        self.deconv_dim = kwargs['NUM_DECONV_FILTERS']
        self._norm_layer = norm_layer
        self.num_joints = 24
        self.norm_type = kwargs['POST']['NORM_TYPE']
        self.depth_dim = kwargs['EXTRA']['DEPTH_DIM']
        self.height_dim = kwargs['HEATMAP_SIZE'][0]
        self.width_dim = kwargs['HEATMAP_SIZE'][1]
        self.smpl_dtype = torch.float32

        backbone = ResNet

        self.preact = backbone(f"resnet{kwargs['NUM_LAYERS']}")

        # Imagenet pretrain model
        import torchvision.models as tm
        if kwargs['NUM_LAYERS'] == 101:
            ''' Load pretrained model '''
            x = tm.resnet101(pretrained=True)
            self.feature_channel = 2048
        elif kwargs['NUM_LAYERS'] == 50:
            x = tm.resnet50(pretrained=True)
            self.feature_channel = 2048
        elif kwargs['NUM_LAYERS'] == 34:
            x = tm.resnet34(pretrained=True)
            self.feature_channel = 512
        elif kwargs['NUM_LAYERS'] == 18:
            x = tm.resnet18(pretrained=True)
            self.feature_channel = 512
        else:
            raise NotImplementedError
        model_state = self.preact.state_dict()
        state = {k: v for k, v in x.state_dict().items()
                 if k in self.preact.state_dict() and v.size() == self.preact.state_dict()[k].size()}
        model_state.update(state)
        self.preact.load_state_dict(model_state)

        self.deconv_layers = self._make_deconv_layer()
        self.final_layer = nn.Conv2d(
            self.deconv_dim[2], self.num_joints * self.depth_dim, kernel_size=1, stride=1, padding=0)

        h36m_jregressor = np.load('./model_files/J_regressor_h36m.npy')
        self.smpl = SMPL_layer(
            './model_files/basicModel_neutral_lbs_10_207_0_v1.0.0.pkl',
            h36m_jregressor=h36m_jregressor,
            dtype=self.smpl_dtype,
            num_joints=self.num_joints
        )

        self.joint_pairs_24 = ((1, 2), (4, 5), (7, 8),
                               (10, 11), (13, 14), (16, 17), (18, 19), (20, 21), (22, 23))
        self.leaf_pairs = ((0, 1), (3, 4))
        self.root_idx_24 = 0

        # mean shape
        init_shape = np.load('./model_files/h36m_mean_beta.npy')
        self.register_buffer(
            'init_shape',
            torch.Tensor(init_shape).float())

        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Linear(self.feature_channel, 1024)
        self.drop1 = nn.Dropout(p=0.5)
        self.fc2 = nn.Linear(1024, 1024)
        self.drop2 = nn.Dropout(p=0.5)
        self.decshape = nn.Linear(1024, 10)
        self.decphi = nn.Linear(1024, 23 * 2)  # [cos(phi), sin(phi)]
        self.decleaf = nn.Linear(1024, 5 * 4)  # rot_mat quat
コード例 #11
0
                       'test.csv')  #.reset_index() # split train - test set.

    start_test = 0
    tensor_transform_test = transforms.Compose([
        CenterCrop(896),
        transforms.ToTensor(),
        lambda x: x.float(),
    ])
    dataset_test = KneeGradingDataset(test,
                                      HOME_PATH,
                                      tensor_transform_test,
                                      stage='test')

    test_loader = data.DataLoader(dataset_test, batch_size=4)
    print('Test data:', len(dataset_test))
    net = resnet34(pretrained=True)
    net.avgpool = nn.AvgPool2d(28, 28)
    net.fc = nn.Sequential(nn.Dropout(0.2), nn.Linear(512, 5))  # OULU's paper.
    net = net.to(device)
    optimizer = optim.Adam(net.parameters(), lr=0.0001, weight_decay=1e-4)
    print(net)
    '''
        # Network load parameter from given directory 
    '''
    if USE_CUDA:
        net.cuda()
        net.load_state_dict(torch.load(model_file_path))
    else:
        net.load_state_dict((torch.load(model_file_path, map_location='cpu')))
    net = nn.DataParallel(net)
    if USE_CUDA:
コード例 #12
0
def main(data_dir, model_name, num, lr, epochs, batch_size=16, download_data=False, save_results=False):

    model_dir = os.path.join("./models", model_name)
    #model_dir='./models/'+model_name
    if not os.path.isdir(model_dir):
        os.mkdir(model_dir)
    print('------start---------')
    model_collections_dict = {
        "resnet34": models.resnet34(),
        #"densenet121": models.densenet121(),

    }
    # Initialize cuda parameters
    use_cuda = torch.cuda.is_available()
    np.random.seed(2020)
    torch.manual_seed(2020)
    device = torch.device("cuda" if use_cuda else "cpu")

    print("Available device = ", device)
    model = model_collections_dict[model_name]
    model.avgpool = torch.nn.AdaptiveAvgPool2d(1)
    '''                                                                     densenet改输出特征数
    num_ftrs = model.classifier.in_features
    model.classifier = torch.nn.Linear(num_ftrs, 40)
    '''
    #'''                                                                     resnet改输出特征数
    num_ftrs = model.fc.in_features
    model.fc = torch.nn.Linear(num_ftrs, 40)                                                                                        #数据集时需改动
    #'''
    model.to(device)
    # Imagnet values
    mean = [0.457342265910642, 0.4387686270106377, 0.4073427106250871]
    std = [0.26753769276329037, 0.2638145880487105, 0.2776826934044154]

    #    mean=[0.485, 0.456, 0.406]
    #    std=[0.229, 0.224, 0.225]

    root = os.getcwd() + '/data/stanford40/'  # 调用图像

    # Load the best weights before testing
    weights_file_path = os.path.join(model_dir, "model-{}.pth".format(num))

    torch.cuda.empty_cache()

    # ---------------Test your model here---------------------------------------
    # Load the best weights before testing
    print("Evaluating model on test set")
    print("Loading best weights")

    model.load_state_dict(torch.load(weights_file_path))
    transformations_test = transforms.Compose([transforms.Resize(330),
                                               transforms.FiveCrop(300),
                                               transforms.Lambda(lambda crops: torch.stack(
                                                   [transforms.ToTensor()(crop) for crop in crops])),
                                               transforms.Lambda(lambda crops: torch.stack(
                                                   [transforms.Normalize(mean=mean, std=std)(crop) for crop in crops])),
                                               ])

    for qwe in classes:
        dataset_test = MyDataset(txt=root + qwe + '_testfortype.txt', transform=transformations_test)
        test_loader = DataLoader(dataset_test, batch_size=3, num_workers=0, shuffle=False)  # int(batch_size / 5)
        losss, mappp = test(model, device, test_loader, returnAllScores=True)
        f = open((qwe+'_test_result.txt'), 'w')
        f.write('test loss:' + str(losss) + '          ' + 'test_ap:' + str(mappp))
        f.close()
コード例 #13
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
    dictionary = Dictionary.load_from_file(args.dictionary_path)
    feature_dict = {}

    ques_net = Question_Classifier(args.bert_mode,
                                   args.bert_pretrain,
                                   num_classes=args.ques_num_classes)
    img_net = Network(backbone_type=args.backbone_type,
                      num_classes=args.img_num_classes)
    cls_net = models.resnet34(pretrained=False, num_classes=2)
    cls_net = cls_net.cuda()
    ques_net = ques_net.cuda()
    img_net = img_net.cuda()

    cls_net.load_state_dict(torch.load(args.cls2_model_path))
    ques_net.load_state_dict(
        torch.load(args.ques_model_path,
                   map_location=lambda storage, loc: storage))
    img_net.load_model(args.img_model_path)

    eval_dset = VQAFeatureDataset(args.split,
                                  dictionary,
                                  args.data_root,
                                  question_len=12,
                                  clip=True)
    eval_loader = DataLoader(eval_dset,
                             args.batch_size,
                             shuffle=False,
                             num_workers=2)

    cls_net.eval()
    ques_net.eval()
    img_net.eval()

    gt_list = []
    with torch.no_grad():
        for v, q, a, ans_type, q_types, image_name in tqdm(iter(eval_loader)):
            v, q, a = v.cuda(), q.cuda(), a.cuda()
            v = v.reshape(v.shape[0], 3, 224, 224)
            q_prob = ques_net(q)  # ques_num_classes
            q_prob = q_prob[
                0]  # [0: closed-ended-normal, 1: closed-ended-abnormal 2: open-ended]
            q_type = torch.argmax(q_prob)

            v_prob, feature = img_net(v)  # 1 x img_num_classes

            if q_type == 0 or q_type == 1:
                continue
            else:
                feature = feature.cpu().numpy().tolist()
                temp_list = []
                for i in feature:
                    temp_list.append(round(i, 4))
                gt = torch.argmax(a[0]).item()
                if gt not in gt_list:
                    gt_list.append(gt)
                    feature_dict[gt] = [temp_list]
                elif gt in gt_list:
                    feature_dict[gt].append(temp_list)
        json.dump(feature_dict, open('feature_dict.json', 'w'))
コード例 #14
0
def make_model():
    resmodel = models.resnet34(pretrained=True)
    #resmodel.cuda()#cuda
    return resmodel
コード例 #15
0
        out = self.relu(out)

        return out


pretrained = True
model_dict = {
    'resnet18': {
        'pretrained': models.resnet18(pretrained=pretrained),
        'block': BasicBlock,
        'layers': [2, 2, 2, 2],
        'groups': 1,
        'width_per_group': 64
    },
    'resnet34': {
        'pretrained': models.resnet34(pretrained=pretrained),
        'block': BasicBlock,
        'layers': [3, 4, 6, 3],
        'groups': 1,
        'width_per_group': 64
    },
    'resnet50': {
        'pretrained': models.resnet50(pretrained=pretrained),
        'block': Bottleneck,
        'layers': [3, 4, 6, 3],
        'groups': 1,
        'width_per_group': 64
    },
    'resnet101': {
        'pretrained': models.resnet101(pretrained=pretrained),
        'block': Bottleneck,
コード例 #16
0
ファイル: Models.py プロジェクト: dxw1997/GitProject
    def __init__(self,
                 in_ch=3,
                 out_ch=1,
                 BN_enable=True,
                 resnet_pretrain=False):
        super().__init__()
        self.BN_enable = BN_enable
        # encoder部分
        # 使用resnet34或50预定义模型,由于单通道入,因此自定义第一个conv层,同时去掉原fc层
        # 剩余网络各部分依次继承
        # 经过测试encoder取三层效果比四层更佳,因此降采样、升采样各取4次
        if backbone == 'resnet34':
            resnet = models.resnet34(pretrained=resnet_pretrain)
            filters = [64, 64, 128, 256, 512]
        elif backbone == 'resnet50':
            resnet = models.resnet50(pretrained=resnet_pretrain)
            filters = [64, 256, 512, 1024, 2048]
        self.firstconv = nn.Conv2d(in_channels=3,
                                   out_channels=64,
                                   kernel_size=7,
                                   stride=2,
                                   padding=3,
                                   bias=False)
        self.firstbn = resnet.bn1
        self.firstrelu = resnet.relu
        self.firstmaxpool = resnet.maxpool
        self.encoder1 = resnet.layer1
        self.encoder2 = resnet.layer2
        self.encoder3 = resnet.layer3

        # decoder部分
        self.center = DecoderBlock(in_channels=filters[3],
                                   mid_channels=filters[3] * 4,
                                   out_channels=filters[3],
                                   BN_enable=self.BN_enable)
        self.decoder1 = DecoderBlock(in_channels=filters[3] + filters[2],
                                     mid_channels=filters[2] * 4,
                                     out_channels=filters[2],
                                     BN_enable=self.BN_enable)
        self.decoder2 = DecoderBlock(in_channels=filters[2] + filters[1],
                                     mid_channels=filters[1] * 4,
                                     out_channels=filters[1],
                                     BN_enable=self.BN_enable)
        self.decoder3 = DecoderBlock(in_channels=filters[1] + filters[0],
                                     mid_channels=filters[0] * 4,
                                     out_channels=filters[0],
                                     BN_enable=self.BN_enable)
        if self.BN_enable:
            self.final = nn.Sequential(
                nn.Conv2d(in_channels=filters[0],
                          out_channels=32,
                          kernel_size=3,
                          padding=1),
                nn.BatchNorm2d(32),
                nn.ReLU(inplace=False),
                nn.Conv2d(in_channels=32, out_channels=1, kernel_size=1),
                #nn.Sigmoid()
            )
        else:
            self.final = nn.Sequential(
                nn.Conv2d(in_channels=filters[0],
                          out_channels=32,
                          kernel_size=3,
                          padding=1),
                nn.ReLU(inplace=False),
                nn.Conv2d(in_channels=32, out_channels=1, kernel_size=1),
                #nn.Sigmoid()
            )
コード例 #17
0
    # calculate edges similarity
    emb_1 = edge_to_embedding(g1)
    emb_2 = edge_to_embedding(g2)
    rank_1 = embedding_to_rank(emb_1)
    rank_2 = embedding_to_rank(emb_2)
    k = emb_1.shape[0]
    edge_similarity = 1 - 6 * np.sum(np.square(rank_1 - rank_2)) / (k**3 - k)

    return vertex_similarity + Lambda * edge_similarity


if __name__ == "__main__":
    from captum.attr import InputXGradient
    from torchvision.models import resnet34

    model_1 = resnet34(num_classes=10)
    graph_1 = get_attribution_graph(model_1,
                                    attribution_type=InputXGradient,
                                    with_noise=False,
                                    probe_data=torch.rand(10, 3, 244, 244),
                                    device=torch.device("cpu"))

    model_2 = resnet34(num_classes=10)
    graph_2 = get_attribution_graph(model_2,
                                    attribution_type=InputXGradient,
                                    with_noise=False,
                                    probe_data=torch.rand(10, 3, 244, 244),
                                    device=torch.device("cpu"))

    print(graph_similarity(graph_1, graph_2))
コード例 #18
0
            transforms.Scale((224, 224)),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val':
        transforms.Compose([
            transforms.Scale((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    bs = 32
    if model_name == 'resnet34':
        modelclc = models.resnet34(pretrained=True)
    if model_name == 'resnet152':
        bs = 8
        modelclc = models.resnet152(pretrained=True)
    if model_name == 'resnet18':
        modelclc = models.resnet18
    if model_name == "resnet50":
        modelclc = models.resnet50(pretrained=True)
    image_datasets = {
        x: MyDataSet(x, data_transforms[x])
        for x in ['train', 'val']
    }
    weights = {
        x: MyDataSet(x, data_transforms[x]).weights
        for x in ['train', 'val']
    }
コード例 #19
0
    def __init__(self, pretrained=False, num_classes=10):
        super(Model, self).__init__()
        # original Resnet
        self.rgb_resnet = models.resnet34(pretrained)
        self.depth_resnet = models.resnet34(pretrained)
        self.ir_resnet = models.resnet34(pretrained)
        self.hsv_resnet = models.resnet34(pretrained)
        self.YCbCr_resnet = models.resnet34(pretrained)
        self.resnet = models.resnet34(pretrained)

        self.rgb_layer0 = nn.Sequential(
            self.rgb_resnet.conv1,
            self.rgb_resnet.bn1,
            self.rgb_resnet.relu,
            self.rgb_resnet.maxpool
        )

        self.rgb_layer1 = self.rgb_resnet.layer1
        self.rgb_layer2 = self.rgb_resnet.layer2
        self.rgb_selayer = SELayer(128)
        self.rgb_qan = QAN()

        self.depth_layer0 = nn.Sequential(
            self.depth_resnet.conv1,
            self.depth_resnet.bn1,
            self.depth_resnet.relu,
            self.depth_resnet.maxpool
        )
        self.depth_layer1 = self.depth_resnet.layer1
        self.depth_layer2 = self.depth_resnet.layer2
        self.depth_selayer = SELayer(128)
        self.depth_qan = QAN()

        self.ir_layer0 = nn.Sequential(
            self.ir_resnet.conv1,
            self.ir_resnet.bn1,
            self.ir_resnet.relu,
            self.ir_resnet.maxpool
        )
        self.ir_layer1 = self.ir_resnet.layer1
        self.ir_layer2 = self.ir_resnet.layer2
        self.ir_selayer = SELayer(128)
        self.ir_qan = QAN()
        
        self.hsv_layer0 = nn.Sequential(
            self.hsv_resnet.conv1,
            self.hsv_resnet.bn1,
            self.hsv_resnet.relu,
            self.hsv_resnet.maxpool
        )
        self.hsv_layer1 = self.hsv_resnet.layer1
        self.hsv_layer2 = self.hsv_resnet.layer2
        self.hsv_selayer = SELayer(128)
        self.hsv_qan = QAN()

        self.ycb_layer0 = nn.Sequential(
            self.YCbCr_resnet.conv1,
            self.YCbCr_resnet.bn1,
            self.YCbCr_resnet.relu,
            self.YCbCr_resnet.maxpool
        )
        self.ycb_layer1 = self.YCbCr_resnet.layer1
        self.ycb_layer2 = self.YCbCr_resnet.layer2
        self.ycb_selayer = SELayer(128)
        self.ycb_qan = QAN()
        
        self.qan = QAN()
        self.layer3 = self.resnet.layer3
        self.layer4 = self.resnet.layer4
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512,2)

        self.catConv = nn.Sequential(
            nn.Conv2d(640, 128, kernel_size=3, stride=1, padding=1,bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
        )
コード例 #20
0
        init.constant(self.conv10.weight, 0)  # Zero init

    def forward(self, x):
        x1, x2, x3, x4, x5 = self.pretrained_net(x)
        x = self.relu(self.bn5(self.conv5(x5)))
        x = self.relu(self.bn6(self.conv6(x + x4)))
        x = self.relu(self.bn7(self.conv7(x + x3)))
        x = self.relu(self.bn8(self.conv8(x + x2)))
        x = self.relu(self.bn9(self.conv9(x + x1)))
        x = self.conv10(x)
        return x


# Training/Testing
pretrained_net = FeatureResNet()
pretrained_net.load_state_dict(models.resnet34(pretrained=True).state_dict())
net = SegResNet(num_classes, pretrained_net).cuda()
crit = nn.BCELoss().cuda()
optimiser = optim.RMSprop(net.parameters(),
                          lr=args.lr,
                          weight_decay=args.weight_decay)
scores, mean_scores = [], []


def train(e):
    net.train()
    for i, (input, target, _) in enumerate(train_loader):
        optimiser.zero_grad()
        input, target = Variable(input.cuda(async=True)), Variable(
            target.cuda(async=True))
        output = F.sigmoid(net(input))
コード例 #21
0
    y2 = size * (cos(pitch) * cos(roll) - sin(pitch) * sin(yaw) * sin(roll)) + tdy

    # Z-Axis (out of the screen) drawn in blue
    x3 = size * (sin(yaw)) + tdx
    y3 = size * (-cos(yaw) * sin(pitch)) + tdy

    cv2.line(img, (int(tdx), int(tdy)), (int(x1), int(y1)), (0, 0, 255), 3)
    cv2.line(img, (int(tdx), int(tdy)), (int(x2), int(y2)), (0, 255, 0), 3)
    cv2.line(img, (int(tdx), int(tdy)), (int(x3), int(y3)), (255, 0, 0), 2)

    return img


####Little myNetwork

resnetmodel = models.resnet34(pretrained=True)


class myNetwork(nn.Module):
    def __init__(self):
        super(myNetwork, self).__init__()

        self.classifier = nn.Sequential(

            nn.Linear(512, 256),
            nn.LeakyReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(256, 3),
        )

    def forward(self, x):
コード例 #22
0
 def create_model(self, n_classes):
     model = models.resnet34(pretrained=False)
     n_features = model.fc.in_features
     model.fc = nn.Linear(n_features, n_classes)
     return model.to(self.device)
コード例 #23
0
    def __init__(self, n_channels, n_classes):
        super(BASNet, self).__init__()

        resnet = models.resnet34(pretrained=True)

        ## -------------Encoder--------------

        self.inconv = nn.Conv2d(n_channels, 64, 3, padding=1)
        self.inbn = nn.BatchNorm2d(64)
        self.inrelu = nn.ReLU(inplace=True)

        #stage 1
        self.encoder1 = resnet.layer1  #224
        #stage 2
        self.encoder2 = resnet.layer2  #112
        #stage 3
        self.encoder3 = resnet.layer3  #56
        #stage 4
        self.encoder4 = resnet.layer4  #28

        self.pool4 = nn.MaxPool2d(2, 2, ceil_mode=True)

        #stage 5
        self.resb5_1 = BasicBlock(512, 512)
        self.resb5_2 = BasicBlock(512, 512)
        self.resb5_3 = BasicBlock(512, 512)  #14

        self.pool5 = nn.MaxPool2d(2, 2, ceil_mode=True)

        #stage 6
        self.resb6_1 = BasicBlock(512, 512)
        self.resb6_2 = BasicBlock(512, 512)
        self.resb6_3 = BasicBlock(512, 512)  #7

        ## -------------Bridge--------------

        #stage Bridge
        self.convbg_1 = nn.Conv2d(512, 512, 3, dilation=2, padding=2)  # 7
        self.bnbg_1 = nn.BatchNorm2d(512)
        self.relubg_1 = nn.ReLU(inplace=True)
        self.convbg_m = nn.Conv2d(512, 512, 3, dilation=2, padding=2)
        self.bnbg_m = nn.BatchNorm2d(512)
        self.relubg_m = nn.ReLU(inplace=True)
        self.convbg_2 = nn.Conv2d(512, 512, 3, dilation=2, padding=2)
        self.bnbg_2 = nn.BatchNorm2d(512)
        self.relubg_2 = nn.ReLU(inplace=True)

        ## -------------Decoder--------------

        #stage 6d
        self.conv6d_1 = nn.Conv2d(1024, 512, 3, padding=1)  # 16
        self.bn6d_1 = nn.BatchNorm2d(512)
        self.relu6d_1 = nn.ReLU(inplace=True)

        self.conv6d_m = nn.Conv2d(512, 512, 3, dilation=2, padding=2)  ###
        self.bn6d_m = nn.BatchNorm2d(512)
        self.relu6d_m = nn.ReLU(inplace=True)

        self.conv6d_2 = nn.Conv2d(512, 512, 3, dilation=2, padding=2)
        self.bn6d_2 = nn.BatchNorm2d(512)
        self.relu6d_2 = nn.ReLU(inplace=True)

        #stage 5d
        self.conv5d_1 = nn.Conv2d(1024, 512, 3, padding=1)  # 16
        self.bn5d_1 = nn.BatchNorm2d(512)
        self.relu5d_1 = nn.ReLU(inplace=True)

        self.conv5d_m = nn.Conv2d(512, 512, 3, padding=1)  ###
        self.bn5d_m = nn.BatchNorm2d(512)
        self.relu5d_m = nn.ReLU(inplace=True)

        self.conv5d_2 = nn.Conv2d(512, 512, 3, padding=1)
        self.bn5d_2 = nn.BatchNorm2d(512)
        self.relu5d_2 = nn.ReLU(inplace=True)

        #stage 4d
        self.conv4d_1 = nn.Conv2d(1024, 512, 3, padding=1)  # 32
        self.bn4d_1 = nn.BatchNorm2d(512)
        self.relu4d_1 = nn.ReLU(inplace=True)

        self.conv4d_m = nn.Conv2d(512, 512, 3, padding=1)  ###
        self.bn4d_m = nn.BatchNorm2d(512)
        self.relu4d_m = nn.ReLU(inplace=True)

        self.conv4d_2 = nn.Conv2d(512, 256, 3, padding=1)
        self.bn4d_2 = nn.BatchNorm2d(256)
        self.relu4d_2 = nn.ReLU(inplace=True)

        #stage 3d
        self.conv3d_1 = nn.Conv2d(512, 256, 3, padding=1)  # 64
        self.bn3d_1 = nn.BatchNorm2d(256)
        self.relu3d_1 = nn.ReLU(inplace=True)

        self.conv3d_m = nn.Conv2d(256, 256, 3, padding=1)  ###
        self.bn3d_m = nn.BatchNorm2d(256)
        self.relu3d_m = nn.ReLU(inplace=True)

        self.conv3d_2 = nn.Conv2d(256, 128, 3, padding=1)
        self.bn3d_2 = nn.BatchNorm2d(128)
        self.relu3d_2 = nn.ReLU(inplace=True)

        #stage 2d

        self.conv2d_1 = nn.Conv2d(256, 128, 3, padding=1)  # 128
        self.bn2d_1 = nn.BatchNorm2d(128)
        self.relu2d_1 = nn.ReLU(inplace=True)

        self.conv2d_m = nn.Conv2d(128, 128, 3, padding=1)  ###
        self.bn2d_m = nn.BatchNorm2d(128)
        self.relu2d_m = nn.ReLU(inplace=True)

        self.conv2d_2 = nn.Conv2d(128, 64, 3, padding=1)
        self.bn2d_2 = nn.BatchNorm2d(64)
        self.relu2d_2 = nn.ReLU(inplace=True)

        #stage 1d
        self.conv1d_1 = nn.Conv2d(128, 64, 3, padding=1)  # 256
        self.bn1d_1 = nn.BatchNorm2d(64)
        self.relu1d_1 = nn.ReLU(inplace=True)

        self.conv1d_m = nn.Conv2d(64, 64, 3, padding=1)  ###
        self.bn1d_m = nn.BatchNorm2d(64)
        self.relu1d_m = nn.ReLU(inplace=True)

        self.conv1d_2 = nn.Conv2d(64, 64, 3, padding=1)
        self.bn1d_2 = nn.BatchNorm2d(64)
        self.relu1d_2 = nn.ReLU(inplace=True)

        ## -------------Bilinear Upsampling--------------
        self.upscore6 = nn.Upsample(scale_factor=32,
                                    mode='bilinear',
                                    align_corners=True)  ###
        self.upscore5 = nn.Upsample(scale_factor=16,
                                    mode='bilinear',
                                    align_corners=True)
        self.upscore4 = nn.Upsample(scale_factor=8,
                                    mode='bilinear',
                                    align_corners=True)
        self.upscore3 = nn.Upsample(scale_factor=4,
                                    mode='bilinear',
                                    align_corners=True)
        self.upscore2 = nn.Upsample(scale_factor=2,
                                    mode='bilinear',
                                    align_corners=True)

        ## -------------Side Output--------------
        self.outconvb = nn.Conv2d(512, 1, 3, padding=1)
        self.outconv6 = nn.Conv2d(512, 1, 3, padding=1)
        self.outconv5 = nn.Conv2d(512, 1, 3, padding=1)
        self.outconv4 = nn.Conv2d(256, 1, 3, padding=1)
        self.outconv3 = nn.Conv2d(128, 1, 3, padding=1)
        self.outconv2 = nn.Conv2d(64, 1, 3, padding=1)
        self.outconv1 = nn.Conv2d(64, 1, 3, padding=1)

        ## -------------Refine Module-------------
        self.refunet = RefUnet(1, 64)
コード例 #24
0
    def __init__(self, pretrain=True):
        super(ResNet34, self).__init__()

        self.resnet = models.resnet34(pretrained=pretrain)

        self.resnet.fc = nn.Linear(512, 1)
コード例 #25
0
#!/usr/bin/env python
# coding: utf-8

# In[1]:

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models

# In[2]:

res34 = models.resnet34(pretrained=True)

# In[10]:

# print(res34)

# In[ ]:

# In[31]:


class Decoderblock(nn.Module):
    def __init__(self,
                 in_channels,
                 out_channels,
                 up_kernel_size=3,
                 up_stride=2,
                 up_padding=1):
        super(Decoderblock, self).__init__()
コード例 #26
0
    model_ft = models.densenet121(args.pretrained)
    model_ft.classifier = nn.Linear(model_ft.classifier.in_features,
                                    len(dset_classes))

elif args.model == "ResNet152":
    model_ft = models.resnet152(args.pretrained)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, len(dset_classes))

elif args.model == "ResNet18":
    model_ft = models.resnet18(args.pretrained)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, len(dset_classes))

elif args.model == "ResNet34":
    model_ft = models.resnet34(args.pretrained)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, len(dset_classes))

elif args.model == "ResNet50":
    model_ft = models.resnet50(args.pretrained)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, len(dset_classes))

elif args.model == "ResNet101":
    model_ft = models.resnet101(args.pretrained)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, len(dset_classes))

elif args.model == "VGG19":
    model_ft = models.vgg19(args.pretrained)
コード例 #27
0
def detect(save_img=False):
    imgsz = (
        320, 192
    ) if ONNX_EXPORT else opt.img_size  # (320, 192) or (416, 256) or (608, 352) for (height, width)
    out, source, weights, half, view_img, save_txt = opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt
    webcam = source == '0' or source.startswith('rtsp') or source.startswith(
        'http') or source.endswith('.txt')

    # Initialize
    device = torch_utils.select_device(
        device='cpu' if ONNX_EXPORT else opt.device)
    cleardir(source)
    cleardir(out)

    # Initialize model
    model = Darknet(opt.cfg, imgsz)
    modelc = models.resnet34(pretrained=True)
    #     modelc = EfficientNet.from_pretrained('efficientnet-b0', num_classes=4)

    # Load weights
    ## Recognition model
    attempt_download(weights)
    if weights.endswith('.pt'):  # pytorch format
        model.load_state_dict(
            torch.load(weights, map_location=device)['model'])
    else:  # darknet format
        load_darknet_weights(model, weights)

    ## Classification model
    num_ftrs = modelc.fc.in_features
    # Here the size of each output sample is set to 2.
    # Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
    modelc.fc = nn.Linear(num_ftrs, 37)  # Generalized 할것
    modelc.load_state_dict(
        torch.load('forServiceTest/weights/37class_resnet34.pt'))

    #     modelc.load_state_dict(torch.load('forServiceTest/weights/efficient.pt'))

    #     # Second-stage classifier
    #     classify = False
    #     if classify:
    #         modelc = torch_utils.load_classifier(name='resnet101', n=4)  # initialize
    #         modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device), strict = False)  # load weights
    #         modelc.to(device).eval()

    # Eval mode
    model.to(device).eval()
    modelc.to(device).eval()

    # Fuse Conv2d + BatchNorm2d layers
    # model.fuse()

    # Export mode
    if ONNX_EXPORT:
        model.fuse()
        img = torch.zeros((1, 3) + imgsz)  # (1, 3, 320, 192)
        f = opt.weights.replace(opt.weights.split('.')[-1],
                                'onnx')  # *.onnx filename
        torch.onnx.export(model,
                          img,
                          f,
                          verbose=False,
                          opset_version=11,
                          input_names=['images'],
                          output_names=['classes', 'boxes'])

        # Validate exported model
        import onnx
        model = onnx.load(f)  # Load the ONNX model
        onnx.checker.check_model(model)  # Check that the IR is well formed
        print(onnx.helper.printable_graph(
            model.graph))  # Print a human readable representation of the graph
        return

    # Half precision
    half = half and device.type != 'cpu'  # half precision only supported on CUDA
    if half:
        model.half()

    # Iterate detection process
    print('Start detection process')
    while True:
        # Wait for source file to arrive.
        if len(os.listdir(source)) < 1:
            #             print("no source")
            #             time.sleep(3)
            continue

        cleardir(out)

        # Set Dataloader
        vid_path, vid_writer = None, None
        if webcam:
            view_img = True
            torch.backends.cudnn.benchmark = True  # set True to speed up constant image size inference
            dataset = LoadStreams(source, img_size=imgsz)
        else:
            save_img = True
            dataset = LoadImages(source, img_size=imgsz)

        # Get names and colors
        names = load_classes(opt.names)
        colors = [[random.randint(0, 255) for _ in range(3)]
                  for _ in range(len(names))]

        # Run inference
        t0 = time.time()
        img = torch.zeros((1, 3, imgsz, imgsz), device=device)  # init img
        _ = model(img.half() if half else img.float()
                  ) if device.type != 'cpu' else None  # run once

        for path, img, im0s, vid_cap in dataset:
            img = torch.from_numpy(img).to(device)
            img = img.half() if half else img.float()  # uint8 to fp16/32
            img /= 255.0  # 0 - 255 to 0.0 - 1.0
            if img.ndimension() == 3:
                img = img.unsqueeze(0)

            # Inference
            t1 = torch_utils.time_synchronized()
            pred = model(img, augment=opt.augment)[0]

            # to float
            if half:
                pred = pred.float()
            # Apply NMS
            pred = non_max_suppression(pred,
                                       opt.conf_thres,
                                       opt.iou_thres,
                                       multi_label=False,
                                       classes=opt.classes,
                                       agnostic=opt.agnostic_nms)
            t2 = torch_utils.time_synchronized()

            # Process detections
            for i, det in enumerate(pred):  # detections for image i
                if webcam:  # batch_size >= 1
                    p, s, im0 = path[i], '%g: ' % i, im0s[i].copy()
                else:
                    p, s, im0 = path, '', im0s

                save_path = str(Path(out) / Path(p).name)
                s += '%gx%g ' % img.shape[2:]  # print string
                #                 gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  #  normalization gain whwh

                if det is not None and len(det):
                    # Rescale boxes from imgsz to im0 size
                    det[:, :4] = scale_coords(img.shape[2:], det[:, :4],
                                              im0.shape).round()

                    # Print results
                    for c in det[:, -1].unique():
                        n = (det[:, -1] == c).sum()  # detections per class
                        s += '%g %ss, ' % (n, names[int(c)])  # add to string

                    # Crop detected images
                    queryDic = {}  # { 'file_name' : [x, y, x, y, id] }
                    for j, x in enumerate(det.cpu().numpy()):
                        h, w = im0s.shape[:2]
                        crop_name = 'crop_%g.jpg' % j

                        coord = x[:4].astype(np.int)
                        crop_img = im0s[coord[1]:coord[3], coord[0]:coord[2]]

                        # Make query dicionary for webserver
                        queryDic[crop_name] = list(coord)

                        assert cv2.imwrite(
                            '%s/%s' % (out, crop_name),
                            crop_img), 'Failure extracting classifier boxes'
#                         print('Crop_%g saved' % j)

#                     print(queryDic)
# Print time (inference + NMS)
                print('%sDone. (%.3fs)' % (s, t2 - t1))
            print('Dectect Done. (%.3fs)' % (time.time() - t0))

            # Classify
            class_names = {
                0: "asahi",
                1: "kronenbourg1664blanc",
                2: "carlsberg",
                3: "cassfresh",
                4: "casslite",
                5: "filitebarley",
                6: "filitefresh",
                7: "guinnessdraught",
                8: "heineken",
                9: "hoegaarden",
                10: "hophouse13",
                11: "kloud",
                12: "kozeldark",
                13: "pilsnerurquell",
                14: "fitz",
                15: "sapporo",
                16: "stellaartois",
                17: "terra",
                18: "tiger",
                19: "tsingtao",
                20: "gompyo",
                21: "malpyo",
                22: "grimbergenblanche",
                23: "macarthur",
                24: "budweiser",
                25: "becks",
                26: "bluemoons",
                27: "sunhanipa",
                28: "jejuwitale",
                29: "jejupellongale",
                30: "kronenbourg1664lager",
                31: "klouddraft",
                32: "filgoodseven",
                33: "filgoodoriginal",
                34: "hiteextracold",
                35: "heungcheongmangcheong",
                36: "somersby"
            }

            preprocess = transforms.Compose([
                transforms.Resize((224, 224)),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
            ])

            # TODO:listdir 개수가 0이면 캔 recognition이 안된 것, 나중에 처리하기.
            crop_list = os.listdir(out)

            if len(crop_list) < 1:
                print('Nothing found')

                web_path = '/home/K/Dev/webserver/result/result.json'
                with open(web_path, 'w') as f:
                    json.dump([], f)
                break

            list.sort(crop_list)

            for crop_name in crop_list:
                img_can = Image.open('%s/%s' % (out, crop_name)).convert('RGB')
                img_can = preprocess(img_can)
                inputs = torch.unsqueeze(img_can, 0)
                inputs = inputs.to(device)
                outputs = modelc(inputs)

                _, preds = torch.max(outputs, 1)
                ### TODO:여기서 전체적으로 일정 컨피던스 아래면 '없다' 판정

                predict = int(preds[0].cpu())
                queryDic[crop_name].append(predict)  # 여기 값에 좌표와 id 담겨있음.

                percentage = torch.nn.functional.softmax(outputs,
                                                         dim=1)[0] * 100
                #                 if percentage[predict].item() > 50:
                print('%-10s -> %-10s : %g' % (crop_name, class_names[predict],
                                               percentage[predict].item()))
#                 print(percentage)
#                 web_path = '/home/K/Dev/webserver/result/result.json'
#                 with open(web_path, 'w') as f:
#                     json.dump(result, f)

            print('Classify Done. (%.3fs)' % (time.time() - t0))

            # Send results to webserver
            send = False

            if send:
                result = []
                val_names = ['x1', 'y1', 'x2', 'y2', 'id']

                for v in sorted(queryDic.values(), key=lambda x: x[0]):
                    result.append(dict(zip(val_names, list(map(int, v)))))

                web_path = '/home/K/Dev/webserver/result/result.json'
                with open(web_path, 'w') as f:
                    json.dump(result, f)


#             # Apply Classifier -> 안먹힘
#             if classify:
#                 pred = apply_classifier(pred, modelc, img, im0s)
        print('\nWaiting for next query...')

        # 디버깅용 소스 이미지 복사
        #             shutil.copyfile('%s/source.izg'source, '%s/source.img' % out)
        cleardir(source)
コード例 #28
0
def main():
    if is_server:
        wandb.login()
    global args, run

    # parse args and set seed
    args = parser.parse_args()
    print("args", args)
    if args.resume is None:
        print("Run w/o checkpoint!")
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    random.seed(args.seed)

    # choose model
    if args.arch == "resnet34":
        model = models.resnet34(pretrained=True)
        model.fc = nn.Linear(512, CLASS_AMOUNT)
    elif args.arch == "resnet50":
        model = models.resnet50(pretrained=True)
        model.fc = nn.Linear(2048, CLASS_AMOUNT)
    elif args.arch == "resnet101":
        model = models.resnet101(pretrained=True)
        model.fc = nn.Linear(2048, CLASS_AMOUNT)
    elif args.arch == "vgg13":
        model = models.vgg13(pretrained=True)
        model.classifier[6] = nn.Linear(4096, CLASS_AMOUNT)
    elif args.arch == "vgg16":
        model = models.vgg16(pretrained=True)
        model.classifier[6] = nn.Linear(4096, CLASS_AMOUNT)
    elif args.arch == "BAM":
        model = ResidualNet('ImageNet', args.depth, CLASS_AMOUNT, 'BAM',
                            args.image_size)
    else:
        model = ResidualNet('ImageNet', args.depth, CLASS_AMOUNT, 'CBAM',
                            args.image_size)

    # define loss function (criterion) and optimizer
    pos_weight_train = torch.Tensor([[
        3.27807486631016, 2.7735849056603774, 12.91304347826087,
        0.6859852476290832, 25.229508196721312
    ]])
    if is_server:
        criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight_train).cuda(
            args.cuda_device)
        sam_criterion_outer = nn.BCELoss(reduction='none').cuda(
            args.cuda_device)
        sam_criterion = nn.BCELoss().cuda(args.cuda_device)
    else:
        criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight_train)
        sam_criterion_outer = nn.BCELoss(reduction='none')
        sam_criterion = nn.BCELoss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 args.lr,
                                 weight_decay=args.weight_decay)

    # load checkpoint
    start_epoch = 0
    if args.resume:
        if os.path.isfile(args.resume):
            # load ImageNet checkpoints:
            load_foreign_checkpoint(model)

            # load my own checkpoints:
            # start_epoch = load_checkpoint(model)
        else:
            print(f"=> no checkpoint found at '{args.resume}'")
            return -1

    config = dict(architecture=f"{args.arch}{args.depth}"
                  if args.arch == "resnet" else args.arch,
                  learning_rate=args.lr,
                  epochs=args.epochs,
                  start_epoch=start_epoch,
                  seed=args.seed,
                  weight_decay=args.weight_decay,
                  batch_size=args.batch_size,
                  momentum=args.momentum,
                  workers=args.workers,
                  prefix=args.prefix,
                  resume=args.resume,
                  evaluate=args.evaluate,
                  lmbd=args.lmbd)
    if is_server:
        run = wandb.init(config=config,
                         project="vol.8",
                         name=args.run_name,
                         tags=args.tags)
    if is_server:
        model = model.cuda(args.cuda_device)
    if is_server:
        wandb.watch(model, criterion, log="all", log_freq=args.print_freq)

    print(
        f'Number of model parameters: {sum([p.data.nelement() for p in model.parameters()])}'
    )
    cudnn.benchmark = True

    # Data loading code
    if args.image_size == 256:
        root_dir = 'data/'
        segm_dir = "images/256ISIC2018_Task1_Training_GroundTruth/"
        size0 = 224
    else:
        root_dir = 'data512/'
        segm_dir = "images/512ISIC2018_Task1_Training_GroundTruth/"
        size0 = 448

    traindir = os.path.join(root_dir, 'train')
    train_labels = os.path.join(root_dir, 'train', 'images_onehot_train.txt')
    valdir = os.path.join(root_dir, 'val')
    val_labels = os.path.join(root_dir, 'val', 'images_onehot_val.txt')
    testdir = os.path.join(root_dir, 'test')
    test_labels = os.path.join(root_dir, 'test', 'images_onehot_test.txt')

    train_dataset = DatasetISIC2018(
        label_file=train_labels,
        root_dir=traindir,
        segm_dir=segm_dir,
        size0=size0,
        perform_flips=True,  # perform flips
        perform_crop=True,  # perform random resized crop with size = 224
        transform=None)

    val_dataset = DatasetISIC2018(label_file=val_labels,
                                  root_dir=valdir,
                                  segm_dir=segm_dir,
                                  size0=size0,
                                  perform_flips=False,
                                  perform_crop=False,
                                  transform=transforms.CenterCrop(size0))

    test_dataset = DatasetISIC2018(label_file=test_labels,
                                   root_dir=testdir,
                                   segm_dir=segm_dir,
                                   size0=size0,
                                   perform_flips=False,
                                   perform_crop=False,
                                   transform=transforms.CenterCrop(size0))

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.workers,
                                               pin_memory=True)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              num_workers=args.workers,
                                              pin_memory=True)

    for epoch in range(start_epoch, start_epoch + args.epochs):
        train(train_loader, model, criterion, sam_criterion,
              sam_criterion_outer, epoch, optimizer)
        validate(val_loader, model, criterion, sam_criterion,
                 sam_criterion_outer, epoch)
        test(test_loader, model, criterion, sam_criterion, sam_criterion_outer,
             epoch)

    save_summary()
    if run is not None:
        run.finish()
コード例 #29
0
ファイル: compute_feats.py プロジェクト: fengweie/dsmil-wsi
def main():
    parser = argparse.ArgumentParser(
        description='Compute TCGA features from SimCLR embedder')
    parser.add_argument('--num_classes',
                        default=2,
                        type=int,
                        help='Number of output classes')
    parser.add_argument('--num_feats',
                        default=512,
                        type=int,
                        help='Feature size')
    parser.add_argument('--batch_size',
                        default=128,
                        type=int,
                        help='Batch size of dataloader')
    parser.add_argument('--num_workers',
                        default=0,
                        type=int,
                        help='Number of threads for datalodaer')
    parser.add_argument('--dataset',
                        default='wsi-tcga-lung',
                        type=str,
                        help='Nanme of dataset')
    parser.add_argument('--backbone',
                        default='resnet18',
                        type=str,
                        help='Embedder backbone')
    parser.add_argument('--magnification',
                        default='20x',
                        type=str,
                        help='Magnification to compute features')
    parser.add_argument('--weights',
                        default=None,
                        type=str,
                        help='Folder of the pretrained weights, simclr/runs/*')
    args = parser.parse_args()

    if args.backbone == 'resnet18':
        resnet = models.resnet18(pretrained=False,
                                 norm_layer=nn.InstanceNorm2d)
        num_feats = 512
    if args.backbone == 'resnet34':
        resnet = models.resnet34(pretrained=False,
                                 norm_layer=nn.InstanceNorm2d)
        num_feats = 512
    if args.backbone == 'resnet50':
        resnet = models.resnet50(pretrained=False,
                                 norm_layer=nn.InstanceNorm2d)
        num_feats = 2048
    if args.backbone == 'resnet101':
        resnet = models.resnet101(pretrained=False,
                                  norm_layer=nn.InstanceNorm2d)
        num_feats = 2048
    for param in resnet.parameters():
        param.requires_grad = False
    resnet.fc = nn.Identity()
    i_classifier = mil.IClassifier(resnet,
                                   num_feats,
                                   output_class=args.num_classes).cuda()

    if args.weights is not None:
        weight_path = os.path.join('simclr', 'runs', args.weights,
                                   'checkpoints', 'model.pth')
    else:
        weight_path = glob.glob('simclr/runs/*/checkpoints/*.pth')[-1]
    state_dict_weights = torch.load(weight_path)
    try:
        state_dict_weights.pop('module.l1.weight')
        state_dict_weights.pop('module.l1.bias')
        state_dict_weights.pop('module.l2.weight')
        state_dict_weights.pop('module.l2.bias')
    except:
        state_dict_weights.pop('l1.weight')
        state_dict_weights.pop('l1.bias')
        state_dict_weights.pop('l2.weight')
        state_dict_weights.pop('l2.bias')
    state_dict_init = i_classifier.state_dict()
    new_state_dict = OrderedDict()
    for (k, v), (k_0, v_0) in zip(state_dict_weights.items(),
                                  state_dict_init.items()):
        name = k_0
        new_state_dict[name] = v
    i_classifier.load_state_dict(new_state_dict, strict=False)

    if args.dataset == 'wsi-tcga-lung':
        bags_path = os.path.join('WSI', 'TCGA-lung', 'pyramid', '*', '*')
    feats_path = os.path.join('datasets', args.dataset)
    os.makedirs(feats_path, exist_ok=True)
    bags_list = glob.glob(bags_path + os.path.sep)
    compute_feats(args, bags_list, i_classifier, feats_path)
コード例 #30
0
 def __init__(self):
     super(CNNEncoder, self).__init__()
     pretrained_model = models.resnet34(pretrained=True)
     self.resnet = Sequential(*list(pretrained_model.children())[:-2])
     self.batchnorm = nn.BatchNorm2d(num_features=512, momentum=0.01)
コード例 #31
0
def resnet34():
    return models.resnet34(pretrained=True)
コード例 #32
0
ファイル: train.py プロジェクト: zju3dv/RVL-Dynamic
from dataset_loader.robotcar import RobotCar
from dataset_loader.sensetime import SenseTime
from dataset_loader.dataloaders import get_train_transforms as get_transforms
from PoseNet import PoseNet, Criterion
from common.utils import AbsoluteCriterion
from common.trainer import Trainer
from config import PoseNetConfig

if __name__ == "__main__":

    print("Parse configuration...")
    configuration = PoseNetConfig()

    # Model
    print("Load model...")
    feature_extractor = models.resnet34(pretrained=True)
    model = PoseNet(feature_extractor, drop_rate=configuration.dropout)
    train_criterion = Criterion(beta=configuration.beta,
                                learn_beta=configuration.learn_beta)
    val_criterion = Criterion(
        learn_beta=False)  # beta = 0.0 to see position accuracy

    # Optimizer
    param_list = [{'params': model.parameters()}]
    param_list.append(
        {'params': [train_criterion.beta, train_criterion.gamma]})

    if configuration.optimizer == 'adam':
        optimizer = optim.Adam(param_list, lr=configuration.lr)

    # Data
コード例 #33
0
def initialize_model(model_name,
                     embedding_dim,
                     feature_extracting,
                     use_pretrained=True):
    if model_name == "densenet161":
        model_ft = models.densenet161(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
        if model_name == "densenet169":
            model_ft = models.densenet161(pretrained=use_pretrained,
                                          memory_efficient=True)
            set_parameter_requires_grad(model_ft, feature_extracting)

            #print(model_ft)
            num_features = model_ft.classifier.in_features
            print(num_features)
            #print(embedding_dim)
            model_ft.classifier = nn.Linear(num_features, embedding_dim)
            #print(model_ft)
    if model_name == "densenet121":
        model_ft = models.densenet121(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    if model_name == "densenet201":
        model_ft = models.densenet201(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    elif model_name == "resnet101":
        model_ft = models.resnet101(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "resnet34":
        model_ft = models.resnet34(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_resnet50":
        model_ft = adl_resnet50(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "inceptionv3":
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext":
        model_ft = se_resnext101_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext50":
        model_ft = se_resnext50_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "googlenet":
        model_ft = models.googlenet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        #print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    elif model_name == "mobilenet2":
        model_ft = models.MobileNetV2(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)

        num_features = model_ft.classifier[1].in_features
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "mnasnet":
        model_ft = mnasnet1_0(pretrained=use_pretrained)
        #model_ft = MnasNet()
        set_parameter_requires_grad(model_ft, feature_extracting)

        print(model_ft.classifier[0])
        print(model_ft.classifier[1])

        num_features = model_ft.classifier[1].in_features
        print(num_features)
        print(embedding_dim)
        #model_ft.classifier[0] = nn.Dropout(p=0.2, inplace=False)
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_googlenet":
        model_ft = GoogLeNet()
        set_parameter_requires_grad(model_ft, feature_extracting)
        print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    else:
        raise ValueError

    return model_ft
コード例 #34
0
def main(args):

    device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
    with torch.no_grad():
        trainset, train_loader = init_dataset(args.train_data_dir,
                                              args.batch_size)
        testset, test_loader = init_dataset(args.test_data_dir,
                                            args.batch_size)

        model_list = []
        for ckpt in args.model:
            if args.use_proto:
                model = init_protonet()
            elif args.use_class:
                class_model = models.resnet34(pretrained=False,
                                              num_classes=1000)
                class_model.load_state_dict(torch.load(ckpt))
                model = FeatModel(class_model)
            elif args.use_distill:
                class_model = models.resnet34(pretrained=False,
                                              num_classes=1000)
                model = DistillModel(class_model, 1000)
                model.load_state_dict(torch.load(ckpt))
                model = FeatModel(model)
            else:
                class_model = models.resnet34(pretrained=False)
                model = FeatModel(class_model)
                model.load_state_dict(torch.load(ckpt))

            model.to(device)
            model_list.append(model)

        feat_novel = torch.zeros((len(trainset), 512))
        label_novel = torch.zeros((len(trainset)))

        feat_query = torch.zeros((len(testset), 512))
        label_query = torch.zeros((len(testset)))

        print('Runing forward on noval images')
        # tr_iter = iter(train_loader)
        for idx, batch in enumerate(tqdm(train_loader)):
            x, y = batch
            x, y = x.to(device), y.to(device)
            model_outputs = [model(x).unsqueeze(0) for model in model_list]
            model_output, _ = torch.max(torch.cat(model_outputs), 0)
            start_idx = idx * args.batch_size
            end_idx = min((idx + 1) * args.batch_size, len(trainset))
            feat_novel[start_idx:end_idx, :] = model_output
            label_novel[start_idx:end_idx] = y

        print('Runing forward on query images')
        for idx, batch in enumerate(tqdm(test_loader)):
            x, y = batch
            x, y = x.cuda(), y.cuda()
            model_output = model(x)
            start_idx = idx * args.batch_size
            end_idx = min((idx + 1) * args.batch_size, len(testset))
            feat_query[start_idx:end_idx, :] = model_output
            label_query[start_idx:end_idx] = y

        labels0 = label_novel.data.cpu().numpy()
        labels1 = label_query.data.cpu().numpy()
        same = labels0 == labels1[:, np.newaxis]
        r, c = np.where(same)

        res = faiss.StandardGpuResources()
        index = faiss.GpuIndexFlatIP(res, 512)
        index.add(feat_novel.data.cpu().numpy())

        #  top 5 precision
        k5 = 5  # we want to see 5 nearest neighbors
        D5, I5 = search_index_pytorch(index, feat_query, k5)
        prec5 = (np.isin(c.reshape(-1, 1), I5[r])).sum() / c.shape[0]

        # top 1 acc
        k1 = 1
        D1, I1 = search_index_pytorch(index, feat_query, k1)
        prec1 = (c.reshape(-1, 1) == I1[r]).sum().item() / c.shape[0]

        print("top 5 precision {}".format(prec5))
        print("top 1 precision {}".format(prec1))
コード例 #35
0
])

# 构建MyDataset实例
train_data = AntsDataset(data_dir=train_dir, transform=train_transform)
valid_data = AntsDataset(data_dir=valid_dir, transform=valid_transform)

# 构建DataLoder
train_loader = DataLoader(dataset=train_data,
                          batch_size=BATCH_SIZE,
                          shuffle=True)
valid_loader = DataLoader(dataset=valid_data, batch_size=BATCH_SIZE)

# ============================ step 2/5 模型 ============================

# 1/3 构建模型
resnet34_ft = models.resnet34()

# 2/3 加载参数
# flag = 0
flag = 1
if flag:
    path_pretrained_model = os.path.join(BASEDIR, "..", "..", "data",
                                         "resnet34-333f7ec4.pth")
    if not os.path.exists(path_pretrained_model):
        raise Exception(
            "\n{} 不存在,请下载 07-02-数据-模型finetune.zip\n放到 {}下,并解压即可".format(
                path_pretrained_model, os.path.dirname(path_pretrained_model)))
    state_dict_load = torch.load(path_pretrained_model)
    resnet34_ft.load_state_dict(state_dict_load)

# 法1 : 冻结卷积层