def __init__(self, args):
        super().__init__()

        self.inp = 64
        self.oup = 64
        self.bifpn_repeat = 2
        print(args.backbone)
        self.backbone = EfficientNet.from_pretrained(args)
        # self.backbone.get_list_features()
        self.tail = nn.ModuleList([
            ConvBlock(320, self.oup, 3, 2, 1),
            ConvBlock(self.oup, self.oup, 3, 2, 1)
        ])
        self.channel_same = self.change_channel(
            self.backbone.get_list_feature()[-3:])
        self.BiFPN_first = BiFPN(oup=self.oup, first=True)
        self.BiFPN = nn.ModuleList()
        for i in range(self.bifpn_repeat - 1):
            self.BiFPN.append(BiFPN(oup=self.oup, first=False))
예제 #2
0
                                        transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=2, shuffle=True)

testset = torchvision.datasets.CIFAR10(root='./data',
                                       train=False,
                                       download=True,
                                       transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=2, shuffle=False)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
           'ship', 'truck')

device = torch.device("cuda:0")
model = efficientnet_pytorch.EfficientNet.from_pretrained('efficientnet-b0')
BPnet = BPnetwork5()
mymodel = EfficientNet.from_pretrained('efficientnet-b0')
myBPnet = BPnetwork5q()
model.to(device)
BPnet.to(device)
mymodel.to(device)
myBPnet.to(device)

# myBPnet.fc1.weight = BPnet.fc1.weight.detach().clone()
# myBPnet.fc1.bias = BPnet.fc1.bias.detach().clone()
myBPnet.fc1.load_state_dict(BPnet.fc1.state_dict())

mycriterion = CrossEntropy()
criterion = nn.CrossEntropyLoss()

optimizer_cnn = optim.RMSprop(model.parameters(), lr=0.0000256, momentum=0.9)
myoptimizer_cnn = optim.RMSprop(mymodel.parameters(),
예제 #3
0
    return pred + 1


def show_result(pred, image):
    """
  Show input image with a result of the test function
  """
    fig = plt.figure()
    title = 'predict: ' + str(pred)
    fig1 = fig.add_subplot(1, 1, 1)
    fig1.set_title(title)
    fig1.axis("off")
    plt.imshow(image)
    fig.tight_layout()
    plt.show()


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

model_name = 'efficientnet-b0'
image_size = EfficientNet.get_image_size(model_name)
model = EfficientNet.from_pretrained(model_name, num_classes=15)

model = model.to(device)
model.load_state_dict(torch.load('best.pt', map_location=device))

image = Image.open('sample.png').convert(
    'RGB')  # directory setting. Change sample.png to path of an input image
pred = test(image)
show_result(pred, image)
예제 #4
0
#
if __name__ == "__main__":
    from config import Config
    from model import EfficientNet
    opt = Config()
    torch.cuda.empty_cache()
    device = torch.device(opt.device)
    criterion = torch.nn.CrossEntropyLoss().cuda()
    model_name = opt.backbone
    model_save_dir = os.path.join(opt.checkpoints_dir, model_name)
    if not os.path.exists(model_save_dir): os.makedirs(model_save_dir)
    logger = get_logger(os.path.join(model_save_dir, 'log.log'))
    logger.info('Using: {}'.format(model_name))
    logger.info('InputSize: {}'.format(opt.input_size))
    logger.info('optimizer: {}'.format(opt.optimizer))
    logger.info('lr_init: {}'.format(opt.lr))
    logger.info('batch size: {}'.format(opt.train_batch_size))
    logger.info('criterion: {}'.format(opt.loss))
    logger.info('Using label smooth: {}'.format(opt.use_smooth_label))
    logger.info('lr_scheduler: {}'.format(opt.lr_scheduler))
    logger.info('Using the GPU: {}'.format(str(opt.gpu_id)))

    model = EfficientNet.from_pretrained(model_name='efficientnet-b7',
                                         num_classes=2)
    model.to(device)
    optimizer = optim.AdamW(model.parameters(), lr=3e-4, weight_decay=5e-4)
    #lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.5)
    lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(
        optimizer, T_0=3, T_mult=2, eta_min=1e-6, last_epoch=-1)
    train_model(model, criterion, optimizer, lr_scheduler=lr_scheduler)
예제 #5
0
    file.write(str(label_no) + '\n')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights',
                        type=str,
                        default=WEIGTH_PATH,
                        help='the weights file you want to test')
    args = parser.parse_args()

    # net = resnet50()
    # num_ftrs = net.fc.in_features
    # net.fc = nn.Linear(num_ftrs, 38)

    net = EfficientNet.from_pretrained('efficientnet-b0', num_classes=39)

    net = net.cuda()

    print("__Resnet load success")

    net.load_state_dict(torch.load(args.weights), True)
    print(net)

    net.eval()

    # __ Walk the exam folder
    folder_path = DATA_EXAM_PATH
    path_list = os.listdir(folder_path)

    # Output .dat file
예제 #6
0
def main():
    parser = argparse.ArgumentParser(description='Transfer images to styles.')
    parser.add_argument('--dataset-dir',
                        type=str,
                        dest='data_dir',
                        help='path to content images directory',
                        default='data/img_tiff')
    parser.add_argument('--chk-path',
                        type=str,
                        dest='weight_path',
                        help='path to model weights',
                        default='none')
    parser.add_argument(
        '--device',
        type=str,
        help='type of inference device',
        default='cuda',
    )
    parser.add_argument('--batch-size', type=int, dest='batch_size', default=4)
    parser.add_argument('--epoch', type=int, dest='num_epoch', default=10)

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

    args = parser.parse_args()
    num_epoch = args.num_epoch

    source_images_path = glob.glob(os.path.join(
        args.data_dir, '*'))  # glob.glob("arg.data_dir/*")

    elbow_xray_dataset = ElbowxrayDataset(xlsx_file='data/elbow.xlsx',
                                          root_dir='data/img_tiff/',
                                          transform=transforms.Compose([
                                              transforms.ToTensor(),
                                              transforms.Resize(size=(512,
                                                                      512))
                                          ]))

    dataloader_train = torch.utils.data.DataLoader(elbow_xray_dataset,
                                                   batch_size=args.batch_size,
                                                   shuffle=True)

    if args.weight_path == 'none':
        model = EfficientNet.from_pretrained('efficientnet-b6',
                                             num_classes=2).to(device)
        model.train()
    else:
        state = torch.load(args.weight_path)
        model.load_state_dict(state)
        model.train()
    optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
    criterion = torch.nn.CrossEntropyLoss()  # subject due to change

    for epoch in range(num_epoch):

        for i, data in enumerate(dataloader_train, 0):
            images, labels = data['image'].to(device), data['label'].to(device)

            optimizer.zero_grad()

            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            if i % 20 == 19:
                print('[Epoch %d/%d, %5d] loss: %.3f' %
                      (epoch + 1, num_epoch, i + 1, loss / 20))

        torch.save(model.state_dict(), 'ckpt/' + str(epoch) + '.pkl')