Ejemplo n.º 1
0
def main():
    global_config = efficientnet_configs['fanout']
    net = EfficientNet(width_coeff=1, depth_coeff=1, dropout=0.2, num_classes=1000, global_config=global_config, out_indices=[2,3,4])
    images = torch.rand((2, 3, 512, 512))
    test_feature_type(net, images)
    test_feature_dimensions(net, images)
    test_feature_info(net, images)
    print("Model Layer Names")
    for n, m in net.named_modules():
        print(n)
Ejemplo n.º 2
0
 def __init__(self,
              compound_coef,
              load_weights=False,
              use_gpu=config['cuda'],
              crop_size=config['crop_size']):
     super(EfficientNet, self).__init__()
     model = EffNet.from_pretrained(f'efficientnet-b{compound_coef}',
                                    load_weights)
     del model._conv_head
     del model._bn1
     # del model._avg_pooling
     # del model._dropout
     update_weight = False
     for name, param in model.named_parameters(
     ):  # nn.Module有成员函数parameters()
         if "_blocks.10" in name:
             update_weight = True
         elif "_blocks.11" in name:
             update_weight = False
         if update_weight is False:
             param.requires_grad = False
     in_features = model._blocks_args[-1].output_filters
     out_features = 128
     model._fc = nn.Linear(in_features, out_features)
     self.model = model
     self.use_gpu = use_gpu
     self.crop_size = crop_size
Ejemplo n.º 3
0
def base_model(images,
               metadata,
               fc_size,
               network_code,
               dropout_rate,
               fc_bottleneck=True):

    is_training = tf.placeholder(tf.bool)
    # create the base pre-trained model

    net = images
    if metadata.get_tensor_size()[-1] != 3:
        with tf.variable_scope("Preprocessing"):
            net = tf.layers.conv2d(net,
                                   filters=3,
                                   kernel_size=[3, 3],
                                   padding="SAME")

    # ** Efficient Net **
    base_model = EfficientNet(model_name=network_code, num_classes=256)

    # use no specific scope
    net, endpoints = base_model.model(net, True)
    net = endpoints['global_pool']
    # net = tf.layers.flatten(net)
    net = tf.reduce_mean(net, [1, 2])

    if fc_bottleneck:
        with tf.variable_scope("FullyConnected_base"):
            # if self.isMultilabel:
            #   net = tf.contrib.layers.fully_connected(net, num_outputs=self.number_of_labels*2)
            #   #net = tf.layers.dropout(net, rate=self.dropout)
            #   net = tf.reshape(net, [-1, self.number_of_labels, 2])
            # else:
            #   net = tf.contrib.layers.fully_connected(net, num_outputs=self.number_of_labels)
            #   #net = tf.layers.dropout(net, rate=self.dropout)
            # net = tf.layers.dropout(
            #   inputs=net, rate=0.3,
            #   training=self.is_training)
            net = tf.layers.dense(inputs=net,
                                  units=fc_size,
                                  activation=tf.nn.relu)
    net = tf.layers.dropout(inputs=net,
                            rate=dropout_rate,
                            training=is_training)

    return net, is_training
Ejemplo n.º 4
0
 def __init__(self, compound_coef, load_weights=False):
     super(EfficientNet, self).__init__()
     model = EffNet.from_pretrained(f'efficientnet-b{compound_coef}', load_weights)
     del model._conv_head
     del model._bn1
     del model._avg_pooling
     del model._dropout
     del model._fc
     self.model = model
Ejemplo n.º 5
0
 def __init__(self, compound_coef, class_num,load_weights=False):
     super(EfficientNet, self).__init__()
     model = EffNet.from_pretrained(f'efficientnet-b{compound_coef}', load_weights, num_classes=class_num)
     # del model._conv_head
     # del model._bn1
     # del model._avg_pooling
     # del model._dropout
     # del model._fc
     self.model = model
Ejemplo n.º 6
0
 def __init__(self):
     super().__init__()
     self.model = EfficientNet.from_pretrained("efficientnet-b1")
     #self.model = EfficientNet.from_name("efficientnet-b0")
     #for param in self.model.parameters():
     #param.requires_grad = True
     self.dp1 = nn.Dropout(p=0.4)
     #self.ln1 = nn.Linear(in_features=1000, out_features=500, bias=True)
     self.ln1 = nn.Linear(in_features=1000, out_features=1, bias=True)
 def __init__(self, compound_coef, load_weights=False, parallel_gpus=None):
     super(EfficientNet, self).__init__()
     model = EffNet.from_pretrained(f'efficientnet-b{compound_coef}',
                                    load_weights,
                                    parallel_gpus=parallel_gpus)
     del model._conv_head
     del model._bn1
     del model._avg_pooling
     del model._dropout
     del model._fc
     self.model = model
     self.use_model_parallel = parallel_gpus is not None
Ejemplo n.º 8
0
    def __init__(self, arch, pretrained=True):
        super().__init__()

        # load EfficientNet
        if arch == 'se_resnext50_32x4d':
            if pretrained:
                self.base = se_resnext50_32x4d()
            else:
                self.base = se_resnext50_32x4d(pretrained=None)
            self.nc = self.base.last_linear.in_features
        elif arch == 'inceptionv4':
            if pretrained:
                self.base = inceptionv4()
            else:
                self.base = inceptionv4(pretrained=None)
            self.nc = self.base.last_linear.in_features
        elif arch == 'inceptionresnetv2':
            if pretrained:
                self.base = inceptionresnetv2()
            else:
                self.base = inceptionresnetv2(pretrained=None)
            self.nc = self.base.last_linear.in_features
        elif 'efficientnet' in arch:
            if pretrained:
                self.base = EfficientNet.from_pretrained(model_name=arch)
            else:
                self.base = EfficientNet.from_name(model_name=arch)

            self.nc = self.base._fc.in_features

        self.logit = nn.Sequential(AdaptiveConcatPool2d(1), Flatten(),
                                   nn.BatchNorm1d(2 * self.nc),
                                   nn.Dropout(0.5),
                                   nn.Linear(2 * self.nc, 512), Mish(),
                                   nn.BatchNorm1d(512), nn.Dropout(0.5),
                                   nn.Linear(512, 1))
Ejemplo n.º 9
0
    def get_trunk(self, architecture):

        if "efficientnet" in architecture.lower():
            self.trunk = EfficientNet.from_pretrained(
                architecture, num_classes=self.MLP_neurons)

        elif "resnet" in architecture.lower():
            if "18" in architecture.lower():
                self.trunk = models.resnet18(pretrained=True)
                self.trunk.fc = nn.Linear(512, self.MLP_neurons)

            elif "50" in architecture.lower():
                self.trunk = models.resnext50_32x4d(pretrained=True)
                self.trunk.fc = nn.Linear(2048, self.MLP_neurons)

        elif "mobilenet" in architecture.lower():
            self.trunk = models.mobilenet_v2(pretrained=True)
            self.trunk.classifier[1] = torch.nn.Linear(1280, self.MLP_neurons)
Ejemplo n.º 10
0
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=100,
                                         shuffle=False,
                                         num_workers=2)

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

# Model
print('==> Building model..')
if model == 'DenseNetWide':
    net = DenseNet(depth=106, k=13, num_classes=10)  # 992,841
if model == 'DenseNetDeep':
    net = DenseNet(depth=117, k=12, num_classes=10)  # 932,986
if model == 'EfficientNetB0':
    net = EfficientNet.from_pretrained('efficientnet-b0',
                                       num_classes=10)  # 4,020,358
if model == 'EfficientNetB4':
    net = EfficientNet.from_pretrained('efficientnet-b4',
                                       num_classes=10)  # 17,566,546
print('Number of parameters: ', count_parameters(net))
net = net.to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
if fine_tune:
    lr = args.lr
    optimizer = optim.SGD(
        [{
            "params":
            chain(net._conv_stem.parameters(), net._bn0.parameters(),
                  net._blocks.parameters()),
Ejemplo n.º 11
0
import time
import numpy as np
from efficientnet import EfficientNet
from tinygrad.tensor import Tensor

if __name__ == "__main__":
    Tensor.default_gpu = True
    model = EfficientNet()

    BS = 4

    img = np.zeros((BS, 3, 224, 224), dtype=np.float32)

    st = time.time()
    out = model.forward(Tensor(img))
    et = time.time()
    print("forward %.2f s" % (et - st))

    y = np.zeros((BS, 1000), np.float32)
    y[range(y.shape[0]), Y] = -1000.0
    y = Tensor(y)
    loss = out.logsoftmax().mul(y).mean()

    st = time.time()
    loss.backward()
    et = time.time()
    print("backward %.2f s" % (et - st))
Ejemplo n.º 12
0
from sklearn.metrics import accuracy_score, roc_auc_score
from tqdm import tqdm
import time
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

opt = TestOptions().parse(print_options=False)
print("{} from {} model testing on {}".format(opt.arch, opt.source_dataset,
                                              opt.target_dataset))

gpu_id = opt.gpu_id
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
use_cuda = torch.cuda.is_available()
print("GPU device %d:" % (gpu_id), use_cuda)

model = EfficientNet.from_name(opt.arch, num_classes=opt.classes)

if opt.resume:
    pretrained = opt.resume
    print("=> using pre-trained model '{}'".format(pretrained))
    model.load_state_dict(torch.load(pretrained)['state_dict'])

model.to('cuda')
cudnn.benchmark = True
print('Total params: %.2fM' % (sum(p.numel()
                                   for p in model.parameters()) / 1000000.0))

criterion = nn.CrossEntropyLoss().cuda()
optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum)

Ejemplo n.º 13
0
parser.add_argument('--epochs', default=10, type=int, help="number of epochs")
parser.add_argument('--timm',
                    action="store_true",
                    help="Use TIMM implementation")

args = parser.parse_args()

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

# Load the model
if args.timm:
    import timm
    model = timm.create_model('efficientnet_b0', pretrained=False)
else:
    model = EfficientNet(Config.B0, num_classes=10)

model.to(device)

# some data fun
transform = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
Ejemplo n.º 14
0
                                            transform=train_transform)
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=batch_size,
                                              shuffle=True,
                                              num_workers=2)

    testset = torchvision.datasets.CIFAR10(root=root,
                                           train=False,
                                           download=True,
                                           transform=transform)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=batch_size,
                                             shuffle=False,
                                             num_workers=2)
    model = EfficientNet.from_name('efficientnet-b0',
                                   first_stride=False,
                                   ktype='oneone',
                                   cifar=False)
    model._fc = nn.Linear(model._fc.in_features, 10)
    model = nn.DataParallel(model)
    model.cuda()
    # checkpoint = torch.load('/home/root1/PycharmProjects/ptt/checkpoint/69/box-can_0_0.8873.torch')
    # model.load_state_dict(checkpoint['net'])
    # del checkpoint
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                init_lr,
                                momentum=0.9,
                                weight_decay=1e-5)

    lr_schudule = MultiStepLR(optimizer, lr_step)
def get_efficientunet_b7(out_channels=2, concat_input=True, pretrained=True):
    encoder = EfficientNet.encoder('efficientnet-b7', pretrained=pretrained)
    model = EfficientUnet(encoder,
                          out_channels=out_channels,
                          concat_input=concat_input)
    return model
Ejemplo n.º 16
0
                int(img_preparam[arch][0] / img_preparam[arch][1]),
                Image.BICUBIC),
            transforms.CenterCrop(img_preparam[arch][0]),
            transforms.ToTensor(), normalize
        ]))

    valid_loader = torch.utils.data.DataLoader(valid_dataset,
                                               batch_size=128,
                                               shuffle=False,
                                               num_workers=16,
                                               pin_memory=False)
    num_batches = int(
        math.ceil(len(valid_loader.dataset) / float(valid_loader.batch_size)))

    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
    model = EfficientNet(arch=arch, num_classes=1000).cuda()
    used_gpus = [idx for idx in range(torch.cuda.device_count())]
    model = torch.nn.DataParallel(model, device_ids=used_gpus).cuda()

    checkpoint = torch.load(
        "/home/liuhuijun/TrainLog/release/imagenet/efficientnet_{}_top1v_86.7.pkl"
        .format(arch))
    pre_weight = checkpoint['model_state']
    model_dict = model.state_dict()
    pretrained_dict = {
        "module." + k: v
        for k, v in pre_weight.items() if "module." + k in model_dict
    }
    model_dict.update(pretrained_dict)
    model.load_state_dict(model_dict)
Ejemplo n.º 17
0
        action='store_true',
        default=False,
        help='if use the official pretrained model, default is True')
    opt = parser.parse_args()
    print(opt)
    gpus = [opt.gpu]
    if len(gpus) == 0:
        device = torch.device('cpu')
    else:
        if gpus[0] >= 0:
            device = torch.device('cuda:%d' % gpus[0])
        else:
            device = torch.device('cpu')
    if 'efficientnet' in opt.arch:
        print('efficientnet image size')
        image_size = EfficientNet.get_image_size(opt.arch)
    else:
        print('not efficientnet image size')
        image_size = opt.imageSize
    dummy_input = torch.randn(64, 3, 224, 224, device=device)
    classi_model = cmodel.ClassiModel(opt.arch,
                                      gpus=[opt.gpu],
                                      num_classes=opt.num_classes,
                                      from_pretrained=opt.from_pretrained)
    if opt.model_url is not None:
        classi_model.loadmodel(opt.model_url, ifload_fc=True)
    torch.onnx.export(classi_model,
                      dummy_input,
                      "eff-b4-s-valid-855.onnx",
                      verbose=True)
Ejemplo n.º 18
0
    ])

    trainset = torchvision.datasets.ImageFolder(root + 'train',
                                                transform=train_transform)
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=batch_size,
                                              shuffle=True,
                                              num_workers=32)

    testset = torchvision.datasets.ImageFolder(root + 'val',
                                               transform=val_transform)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=batch_size,
                                             shuffle=False,
                                             num_workers=8)
    model = EfficientNet.from_name('efficientnet-b0')
    model = nn.DataParallel(model)
    model.cuda()
    # checkpoint = torch.load('/home/root1/PycharmProjects/ptt/checkpoint/69/box-can_0_0.8873.torch')
    # model.load_state_dict(checkpoint['net'])
    # del checkpoint
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(),
                                1e-2,
                                momentum=0.9,
                                weight_decay=1e-5)

    lr_schudule = ExponentialLR(optimizer, 0.97)

    def train(train_loader, model, criterion, optimizer):
Ejemplo n.º 19
0
transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

data_dir = '~/data'
trainset = torchvision.datasets.CIFAR10(root=data_dir, train=True, download=False)
trainset = MultiTransDataset(trainset, transform_big, transform_small)
testset = torchvision.datasets.CIFAR10(root=data_dir, train=False, download=False, transform=transform_test)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)

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

net_big = EfficientNet.from_pretrained('efficientnet-b4', num_classes=10).to(device)
net_big = torch.nn.DataParallel(net_big)
cudnn.benchmark = True

checkpoint = torch.load(checkpoint_dir_big)
net_big.load_state_dict(checkpoint['net'])
net_big.eval()


if args.model == 'DenseNetWide': net_small = DenseNet(depth=106, k=13, num_classes=10).to(device) # <1m
if args.model == 'DenseNetDeep': net_small = DenseNet(depth=117, k=12, num_classes=10).to(device) # <1m

if not os.path.isdir(checkpoint_dir):
    os.mkdir(checkpoint_dir)
log_file_name = os.path.join(checkpoint_dir, 'log.txt')
log_file = open(log_file_name, "at")