Пример #1
0
 def __init__(self, module_2):
     super(Module1, self).__init__()
     self.layer1 = nn.Sequential(
         nn.MaxPool2d(kernel_size=2, stride=2),  # 2x
         Inception(128, 32, [(3, 32, 32), (5, 32, 32), (7, 32, 32)]),
         Inception(128, 32, [(3, 32, 32), (5, 32, 32), (7, 32, 32)]),
         module_2,
         Inception(128, 32, [(3, 64, 32), (5, 64, 32), (7, 64, 32)]),
         Inception(128, 16, [(3, 32, 16), (7, 32, 16), (11, 32, 16)]),
         Interpolate(scale_factor=2, mode='nearest'))
     self.layer2 = nn.Sequential(
         Inception(128, 16, [(3, 64, 16), (7, 64, 16), (11, 64, 16)]))
 def __init__(self):
     super(Module4, self).__init__()
     self.layer1 = nn.Sequential(
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]))
     self.layer2 = nn.Sequential(
         nn.AvgPool2d(kernel_size=2, stride=2),
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Interpolate(scale_factor=2,
                     mode='nearest')  # Up to 8x, 256 channel
     )
Пример #3
0
def main():
    if len(sys.argv) > 1:
        image_to_open = sys.argv[1]
    else:
        response = requests.get("https://picsum.photos/1080")
        image_to_open = BytesIO(response.content)
    image = np.float32(Image.open(image_to_open))

    model = Inception()
    
    if LAYER_INDICES is None:
        optimizations_to_perform = random.randrange(MIN_OPERATIONS, MAX_OPERATIONS)
        layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER), optimizations_to_perform)
    else:
        layer_indices = LAYER_INDICES
        
    final_image = image
    deep_dream = DeepDream(model)
    for i, layer_index in enumerate(layer_indices):
        print("LAYER " + model.layer_names[layer_index] + ", " + str(i) + " out of " + str(len(layer_indices)))
        layer = model.layers[layer_index]
        final_image = deep_dream.recursively_optimize(layer=layer,
                                                      image=final_image,
                                                      iterations=ITERATIONS,
                                                      step_size=STEP_SIZE,
                                                      rescale_factor=RESCALE_FACTOR,
                                                      levels=LEVELS,
                                                      blend=BLEND)
    save_image(final_image, OUTPUT_IMAGE_NAME + ".jpeg")
Пример #4
0
def network(image):
	conv = layers.Conv2d(inputs=image, filters=64, kernel_size=(7,7), strides=(2,2), name="1")
	max_pool = layers.maxPool(inputs=conv, kernel_size=(3,3), strides=(2,2),padding="same")
	red=layers.Conv2d(inputs=max_pool, filters=64,kernel_size=(1,1), strides=(1,1), name="3reduce")
	conv1 = layers.Conv2d(inputs=red, filters=192, kernel_size=(3,3), strides=(1,1), name="3")
	max_pool1 = layers.maxPool(inputs=conv1, kernel_size=(3,3), strides=(2,2),padding="same")
	inception3a = Inception.inception_module(inputs=max_pool1,one_filters=64,three_filters=128,five_filters=32,three_filter_reduce=96,five_filter_reduce=16,pool_proj_filters=32,name="inception3a")
	inception3b = Inception.inception_module(inputs=inception3a,one_filters=128,three_filters=192,five_filters=96,three_filter_reduce=128,five_filter_reduce=32,pool_proj_filters=64,name="inception3b")
	max_pool2 = layers.maxPool(inputs=inception3b, kernel_size=(3,3), strides=(2,2),padding="same")
	inception4a = Inception.inception_module(inputs=max_pool2,one_filters=192,three_filters=208,five_filters=48,three_filter_reduce=96,five_filter_reduce=16,pool_proj_filters=64,name="inception4a")
	# aux net 
	aux_net1_avgpool = layers.avgPool(inputs=inception4a, kernel_size=(5,5), stride=(3,3),padding="same")
	aux_net1_conv = layers.Conv2d(inputs=aux_net1_avgpool, filters=128, kernel_size=(1,1), strides=(2,2),name="aux1_conv")
	aux_net1_flatten = layers.Flatten(inputs=aux_net1_conv)
	aux_net1_dense1 = layers.Dense(units=1024, activation="relu", inputs=aux_net1_flatten,name="aux_net1_dense")
	aux_net1_dense2  = layers.Dense(units=1024, activation="softmax", inputs=aux_net1_dense1,name="aux_net1_dense2")

	inception4b = Inception.inception_module(inputs=inception4a,one_filters=160,three_filters=224,five_filters=64,three_filter_reduce=112,five_filter_reduce=24,pool_proj_filters=64,name="inception4b")
	inception4c = Inception.inception_module(inputs=inception4b,one_filters=128,three_filters=256,five_filters=64,three_filter_reduce=128,five_filter_reduce=64,pool_proj_filters=64,name="inception4c")
	inception4d = Inception.inception_module(inputs=inception4c,one_filters=112,three_filters=288,five_filters=64,three_filter_reduce=144,five_filter_reduce=32,pool_proj_filters=64,name="inception4d")
	# aux net 
	aux_net2_avgpool = layers.avgPool(inputs=inception4d, kernel_size=(5,5), stride=(3,3),padding="same")
	aux_net2_conv = layers.Conv2d(inputs=aux_net2_avgpool, filters=128, kernel_size=(1,1), strides=(2,2),name="aux1_conv")
	aux_net2_flatten = layers.Flatten(inputs=aux_net2_conv)
	aux_net2_dense1 = layers.Dense(units=1024, activation="relu", inputs=aux_net2_flatten,name="aux_net1_dense")
	aux_net2_dense2  = layers.Dense(units=1024, activation="softmax", inputs=aux_net2_dense1,name="aux_net1_dense2")


	inception4e = Inception.inception_module(inputs=inception4d,one_filters=256,three_filters=320,five_filters=128,three_filter_reduce=160,five_filter_reduce=32,pool_proj_filters=128,name="inception4e")
	max_pool2 = layers.maxPool(inputs=inception4e, kernel_size=(3,3), strides=(2,2),padding="same")
	inception5a =Inception.inception_module(inputs=max_pool2,one_filters=256,three_filters=320,five_filters=128,three_filter_reduce=160,five_filter_reduce=32,pool_proj_filters=128,name="inception5a")
	inception5b = Inception.inception_module(inputs=inception5a,one_filters=384,three_filters=384,five_filters=128,three_filter_reduce=192,five_filter_reduce=48,pool_proj_filters=128,name="inception5b")
	avg_pool = layers.avgPool(inputs=inception5b,kernel_size=(7,7),stride=(1,1),padding="same")
	#add drop out
	# flatten = layers.Flatten(inputs=avg_pool)
	dropout = layers.Dropout(inputs=avg_pool,rate=0.4)
	flatten = layers.Flatten(inputs=dropout)
	dense = layers.Dense(units=10,activation="softmax",inputs=flatten,name="dense")
	return dense
Пример #5
0
def main():
    if len(sys.argv) > 1:
        dir_to_open_right, dir_to_open_left, dir_to_render_right, dir_to_render_left = sys.argv[
            1], sys.argv[2], sys.argv[3], sys.argv[4]

    else:
        response = requests.get("https://picsum.photos/1080")
        image_to_open = BytesIO(response.content)

    model = Inception()
    deep_dream = DeepDream(model)

    for image_to_open_right, image_to_open_left in zip(
            os.listdir(dir_to_open_right), os.listdir(dir_to_open_left)):
        image_right, image_left = np.float32(
            Image.open(os.path.join(
                dir_to_open_right, image_to_open_right))), np.float32(
                    Image.open(
                        os.path.join(dir_to_open_left, image_to_open_left)))

        if LAYER_INDICES is None:
            optimizations_to_perform = random.randrange(
                MIN_OPERATIONS, MAX_OPERATIONS)
            layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER),
                                          optimizations_to_perform)
        else:
            layer_indices = LAYER_INDICES

        right_image = image_right
        for i, layer_index in enumerate(layer_indices):
            print("LAYER " + model.layer_names[layer_index] + ", " + str(i) +
                  " out of " + str(len(layer_indices)))
            layer = model.layers[layer_index]
            right_image, left_image = deep_dream.recursively_optimize(
                layer=layer,
                image=right_image,
                left_image=image_left,
                iterations=ITERATIONS,
                step_size=STEP_SIZE,
                rescale_factor=RESCALE_FACTOR,
                levels=LEVELS,
                blend=BLEND)
        save_image(right_image,
                   dir_to_render_right + "/" + image_to_open_right + ".jpeg")
        save_image(left_image,
                   dir_to_render_left + "/" + image_to_open_left + ".jpeg")
Пример #6
0
    def __init__(self):
        super(GoogLeNet, self).__init__()

        self.conv1 = Conv2d(3,
                            64,
                            bias=False,
                            kernel_size=7,
                            stride=2,
                            padding=3)
        self.bn1 = BatchNorm2d(64, eps=0.001)
        self.maxpool1 = MaxPool2d(3, stride=2, ceil_mode=True)

        self.conv2 = Conv2d(64, 64, bias=False, kernel_size=1)
        self.bn2 = BatchNorm2d(64, eps=0.001)

        self.conv3 = Conv2d(64, 192, bias=False, kernel_size=3, padding=1)
        self.bn3 = BatchNorm2d(192, eps=0.001)
        self.maxpool2 = MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
        self.maxpool3 = MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)
        self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)
        self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)
        self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)
        self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)
        self.maxpool4 = MaxPool2d(2, stride=2, ceil_mode=True)

        self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)
        self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)

        self.relu = ReLU(inplace=True)
        self.avgpool = AdaptiveAvgPool2d((1, 1))
        self.dropout = Dropout(0.2)
        self.fc = Linear(1024, 120)
Пример #7
0
def get_model(model_name, num_classes):
    if model_name == "create_new_model":
        return create_new_model(num_classes)
    elif model_name == "AlexNet":
        return AlexNet(num_classes)
    elif model_name == "LeNet5":
        return LeNet5(num_classes)
    elif model_name == "VGG16":
        return VGG16(num_classes)
    elif model_name == "ResNet50":
        return ResNet50(num_classes)
    elif model_name == "InceptionV3":
        return InceptionV3(num_classes)
    elif model_name == "DeepFace":
        return DeepFace(num_classes)
    elif model_name == "VGGFace":
        return VGGFace(num_classes)
    elif model_name == "Inception":
        return Inception(num_classes)
Пример #8
0
 def __init__(self, module_3):
   super(Module2, self).__init__()
   self.layer1 = nn.Sequential(
                   nn.MaxPool2d(kernel_size=2, stride=2), # 4x
                   Inception(128, 32, [(3,32,32), (5,32,32), (7,32,32)]),
                   Inception(128, 64, [(3,32,64), (5,32,64), (7,32,64)]),
                   module_3,
                   Inception(256, 64, [(3,32,64), (5,32,64), (7,32,64)]),
                   Inception(256, 32, [(3,32,32), (5,32,32), (7,32,32)]),
                   Interpolate(scale_factor=2, mode='nearest') # up to 2x, output is 128 channel
                 )
   self.layer2 = nn.Sequential(
                   Inception(128, 32, [(3,32,32), (5,32,32), (7,32,32)]),
                   Inception(128, 32, [(3,64,32), (7,64,32), (11,64,32)])
                 )
 def __init__(self, module_4):
     super(Module3, self).__init__()
     self.layer1 = nn.Sequential(
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 64, 64), (7, 64, 64), (11, 64, 64)]))
     self.layer2 = nn.Sequential(
         nn.AvgPool2d(kernel_size=2, stride=2),  # 8x
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         module_4,  # down 16x then up to 8x
         Inception(256, 64, [(3, 32, 64), (5, 32, 64), (7, 32, 64)]),
         Inception(256, 64, [(3, 64, 64), (7, 64, 64), (11, 64, 64)]),
         Interpolate(scale_factor=2,
                     mode='nearest')  # up to 4x. 256 channel
     )
Пример #10
0
def main():
    if FLAGS.cnn_type == 'inception':
        cnn = Inception()
    else:
        cnn = VGG(FLAGS.nr_feat_maps, FLAGS.tensor_names, FLAGS.image_size)
        # cnn.printTensors()

    dict_test = {}
    dict_test["video_path"] = []
    dict_test["label"] = []
    with open(FLAGS.test_list) as f:
        for line in f:
            video_path, label = line.split()
            dict_test["video_path"].append(video_path)
            dict_test["label"].append(label)

    test_data = pd.DataFrame(data=dict_test, columns=['video_path', 'label'])

    if not os.path.exists(FLAGS.test_data_file):
        test_data.to_csv(FLAGS.test_data_file, sep=',')

    dict = {}
    dict["video_path"] = []
    dict["label"] = []
    dict["feat_path"] = []
    with open(FLAGS.train_list) as f:
        for line in f:
            video_path, label = line.split()
            dict["video_path"].append(video_path)
            dict["label"].append(label)
            dict["feat_path"].append(video_path.split("/")[-1] + ".pkl")

    train_data = pd.DataFrame(data=dict,
                              columns=['video_path', 'label', "feat_path"])

    if not os.path.exists(FLAGS.train_data_file):
        train_data.to_csv(FLAGS.train_data_file, sep=',')

    train_data.apply(lambda row: process_record(cnn, row), axis=1)
Пример #11
0
def main():
    if len(sys.argv) > 1:
        dir_to_open, render_fold = sys.argv[1], sys.argv[2]
    else:
        response = requests.get("https://picsum.photos/1080")
        image_to_open = BytesIO(response.content)

    model = Inception()
    deep_dream = DeepDream(model)

    for file_in_dir in os.listdir(dir_to_open):
        image = np.float32(Image.open(os.path.join(dir_to_open, file_in_dir)))

        if LAYER_INDICES is None:
            optimizations_to_perform = random.randrange(
                MIN_OPERATIONS, MAX_OPERATIONS)
            layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER),
                                          optimizations_to_perform)
        else:
            layer_indices = LAYER_INDICES
        print(20 * ">", layer_indices)
        final_image = image
        for i, layer_index in enumerate(layer_indices):
            print("LAYER " + model.layer_names[layer_index] + ", " + str(i) +
                  " out of " + str(len(layer_indices)))
            layer = model.layers[layer_index]
            final_image = deep_dream.recursively_optimize(
                layer=layer,
                image=final_image,
                iterations=ITERATIONS,
                step_size=STEP_SIZE,
                rescale_factor=RESCALE_FACTOR,
                levels=LEVELS,
                blend=BLEND)
        save_image(final_image,
                   render_fold + "/" + file_in_dir + "_dreamed.jpeg")
Пример #12
0
def train_function(gpu, world_size, node_rank, gpus):
    import torch.multiprocessing
    torch.multiprocessing.set_sharing_strategy('file_system')

    torch.manual_seed(25)
    np.random.seed(25)

    rank = node_rank * gpus + gpu
    dist.init_process_group(
        backend='nccl',
        init_method='env://',
        world_size=world_size,
        rank=rank
    )

    width_size = 512
    batch_size = 32
    accumulation_step = 5
    device = torch.device("cuda:{}".format(gpu) if torch.cuda.is_available() else "cpu")

    if rank == 0:
        wandb.init(project='inception_v3', group=wandb.util.generate_id())
        wandb.config.width_size = width_size
        wandb.config.aspect_rate = 1
        wandb.config.batch_size = batch_size
        wandb.config.accumulation_step = accumulation_step

        shutil.rmtree('tensorboard_runs', ignore_errors=True)
        writer = SummaryWriter(log_dir='tensorboard_runs', filename_suffix=str(time.time()))

    ranzcr_df = pd.read_csv('train_folds.csv')
    ranzcr_train_df = ranzcr_df[ranzcr_df['fold'] != 1]

    chestx_df = pd.read_csv('chestx_pseudolabeled_data_lazy_balancing.csv')
    train_image_transforms = alb.Compose([
        alb.ImageCompression(quality_lower=65, p=0.5),
        alb.HorizontalFlip(p=0.5),
        alb.CLAHE(p=0.5),
        alb.OneOf([
            alb.GridDistortion(
                num_steps=8,
                distort_limit=0.5,
                p=1.0
            ),
            alb.OpticalDistortion(
                distort_limit=0.5,
                shift_limit=0.5,
                p=1.0,
            ),
            alb.ElasticTransform(alpha=3, p=1.0)],
            p=0.7
        ),
        alb.RandomResizedCrop(
            height=width_size,
            width=width_size,
            scale=(0.8, 1.2),
            p=0.7
        ),
        alb.RGBShift(p=0.5),
        alb.RandomSunFlare(p=0.5),
        alb.RandomFog(p=0.5),
        alb.RandomBrightnessContrast(p=0.5),
        alb.HueSaturationValue(
            hue_shift_limit=20,
            sat_shift_limit=20,
            val_shift_limit=20,
            p=0.5
        ),
        alb.ShiftScaleRotate(shift_limit=0.025, scale_limit=0.1, rotate_limit=20, p=0.5),
        alb.CoarseDropout(
            max_holes=12,
            min_holes=6,
            max_height=int(width_size / 6),
            max_width=int(width_size / 6),
            min_height=int(width_size / 6),
            min_width=int(width_size / 20),
            p=0.5
        ),
        alb.IAAAdditiveGaussianNoise(loc=0, scale=(2.5500000000000003, 12.75), per_channel=False, p=0.5),
        alb.IAAAffine(scale=1.0, translate_percent=None, translate_px=None, rotate=0.0, shear=0.0, order=1, cval=0,
                      mode='reflect', p=0.5),
        alb.IAAAffine(rotate=90., p=0.5),
        alb.IAAAffine(rotate=180., p=0.5),
        alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2()
    ])
    train_set = NoisyStudentDataset(ranzcr_train_df, chestx_df, train_image_transforms,
                                    '../ranzcr/train', '../data', width_size=width_size)
    train_sampler = DistributedSampler(train_set, num_replicas=world_size, rank=rank, shuffle=True)
    train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=4, sampler=train_sampler)

    ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1]
    valid_image_transforms = alb.Compose([
        alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2()
    ])
    valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size)
    valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, pin_memory=False, drop_last=False)

    # ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1]
    # valid_image_transforms = alb.Compose([
    #     alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
    #     ToTensorV2()
    # ])
    # valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size)
    # valid_sampler = DistributedSampler(valid_set, num_replicas=world_size, rank=rank)
    # valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, sampler=valid_sampler)

    checkpoints_dir_name = 'inception_v3_noisy_student_{}'.format(width_size)
    os.makedirs(checkpoints_dir_name, exist_ok=True)

    # model = EfficientNetNoisyStudent(11, pretrained_backbone=True,
    #                                  mixed_precision=True, model_name='tf_efficientnet_b7_ns')
    model = Inception(11, pretrained_backbone=True, mixed_precision=False, model_name='inception_v3')
    model = SyncBatchNorm.convert_sync_batchnorm(model)
    model.to(device)
    model = DistributedDataParallel(model, device_ids=[gpu])

    # class_weights = [354.625, 23.73913043478261, 2.777105767812362, 110.32608695652173,
    #                  52.679245283018865, 9.152656621728786, 4.7851333032083145,
    #                  8.437891632878731, 2.4620064899945917, 0.4034751151063363, 31.534942820838626]
    class_names = ['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal',
                   'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',
                   'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present']
    scaler = GradScaler()
    criterion = torch.nn.BCEWithLogitsLoss()

    lr_start = 1e-4
    lr_end = 1e-6
    weight_decay = 0
    epoch_num = 20
    if rank == 0:
        wandb.config.model_name = checkpoints_dir_name
        wandb.config.lr_start = lr_start
        wandb.config.lr_end = lr_end
        wandb.config.weight_decay = weight_decay
        wandb.config.epoch_num = epoch_num
        wandb.config.optimizer = 'adam'
        wandb.config.scheduler = 'CosineAnnealingLR'
        wandb.config.is_loss_weights = 'no'

    optimizer = Adam(model.parameters(), lr=lr_start, weight_decay=weight_decay)
    scheduler = CosineAnnealingLR(optimizer, T_max=epoch_num, eta_min=lr_end, last_epoch=-1)

    max_val_auc = 0

    for epoch in range(epoch_num):
        train_loss, train_avg_auc, train_auc, train_rocs, train_data_pr, train_duration = one_epoch_train(
            model, train_loader, optimizer, criterion, device, scaler,
            iters_to_accumulate=accumulation_step, clip_grads=False)
        scheduler.step()

        if rank == 0:
            val_loss, val_avg_auc, val_auc, val_rocs, val_data_pr, val_duration = eval_model(
                model, valid_loader, device, criterion, scaler)

            wandb.log({'train_loss': train_loss, 'val_loss': val_loss,
                       'train_auc': train_avg_auc, 'val_auc': val_avg_auc, 'epoch': epoch})
            for class_name, auc1, auc2 in zip(class_names, train_auc, val_auc):
                wandb.log({'{} train auc'.format(class_name): auc1,
                           '{} val auc'.format(class_name): auc2, 'epoch': epoch})

            if val_avg_auc > max_val_auc:
                max_val_auc = val_avg_auc
                wandb.run.summary["best_accuracy"] = val_avg_auc

            print('EPOCH %d:\tTRAIN [duration %.3f sec, loss: %.3f, avg auc: %.3f]\t\t'
                  'VAL [duration %.3f sec, loss: %.3f, avg auc: %.3f]\tCurrent time %s' %
                  (epoch + 1, train_duration, train_loss, train_avg_auc,
                   val_duration, val_loss, val_avg_auc, str(datetime.now(timezone('Europe/Moscow')))))

            torch.save(model.module.state_dict(),
                       os.path.join(checkpoints_dir_name, '{}_epoch{}_val_auc{}_loss{}_train_auc{}_loss{}.pth'.format(
                           checkpoints_dir_name, epoch + 1, round(val_avg_auc, 3), round(val_loss, 3),
                           round(train_avg_auc, 3), round(train_loss, 3))))
    if rank == 0:
        wandb.finish()
Пример #13
0
    def post(self, request):
        url_name = resolve(self.request.path).url_name
        print("POST REQUEST:", url_name)
        data = request.data
        print("Data", data)

        if url_name == 'feedback':
            query = data.get("query")
            Keyword(keyword=query).save()
            step = data.get("step")
            selected_images = [
                image_name_from_path(img) for img in data.get("selectedImages")
            ]
            not_selected_images = [
                image_name_from_path(img)
                for img in data.get("nonSelectedImages")
            ]

            self.feedback_parser.save_feedback(query, step, selected_images,
                                               not_selected_images)

            if not selected_images:
                images = relevant_images_based_on_feedback(
                    query, self.feedback_parser.df_feedback)
                if images is None:
                    images = random_images(20)
            else:
                images = similar_images_filter_negative_feedback(
                    query, selected_images, self.feedback_parser.df_feedback,
                    20)

            return Response(self.format_response(query, step + 1, images))

        elif url_name == 'upload_example_image':
            global annoy_index, inception_object
            b64str = data.get('base64image')
            query = data.get("query").lower().strip()
            img = image_from_base64str(b64str, path_to_uploads(query))

            if inception_object is None:
                inception_object = Inception()
                print("INCEPTION OBJECT INITIALIZED")
            if annoy_index is None:
                data, img_lst = load_data_np(inception_layer_path)
                annoy_index = build_Index(data,
                                          vector_size=len(data[0]),
                                          metric="euclidean",
                                          trees=20)
                print("ANNOY INDEX MADE")
                data = []
            n = gc.collect()

            # this is for igor test
            # vector = get_ol_vector(inception_object, img)
            # this is transfer layer
            vector = get_tl_vector(inception_object, img)

            similar_img_indexes = annoy_index.get_nns_by_vector(vector, n=52)
            print("SIM indexes:", similar_img_indexes)
            similar_imgs = [load_images_list()[i] for i in similar_img_indexes]
            print("SIM images:", similar_imgs)

            return Response(self.format_response(query, 0, similar_imgs))

        return Response({"error": "post error"})
# ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1]
# valid_image_transforms = alb.Compose([
#     alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
#     ToTensorV2()
# ])
# valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size)
# valid_sampler = DistributedSampler(valid_set, num_replicas=world_size, rank=rank)
# valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, sampler=valid_sampler)

checkpoints_dir_name = 'inception_v3_noisy_student_{}'.format(width_size)
os.makedirs(checkpoints_dir_name, exist_ok=True)

# model = EfficientNetNoisyStudent(11, pretrained_backbone=True,
#                                  mixed_precision=True, model_name='tf_efficientnet_b7_ns')
model = Inception(11,
                  pretrained_backbone=True,
                  mixed_precision=False,
                  model_name='inception_v3')

# class_weights = [354.625, 23.73913043478261, 2.777105767812362, 110.32608695652173,
#                  52.679245283018865, 9.152656621728786, 4.7851333032083145,
#                  8.437891632878731, 2.4620064899945917, 0.4034751151063363, 31.534942820838626]
class_names = [
    'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', 'NGT - Abnormal',
    'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',
    'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal',
    'Swan Ganz Catheter Present'
]
scaler = GradScaler()
if torch.cuda.device_count() > 1:
    model = torch.nn.DataParallel(model)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
                        num_workers=12,
                        pin_memory=True,
                        drop_last=True)

checkpoints_dir_name = 'tf_efficientnet_b5_ns_{}_no_wd'.format(width_size)
os.makedirs(checkpoints_dir_name, exist_ok=True)
wandb.config.model_name = checkpoints_dir_name

# model = ResNet18(11, 1, pretrained_backbone=True, mixed_precision=True)
# model = EfficientNet(11, pretrained_backbone=True, mixed_precision=True, model_name='tf_efficientnet_b5_ns')
# checkpoint_path='tf_efficientnet_b7_ns_pretrain_600/tf_efficientnet_b7_ns_pretrain_600_epoch6_val_auc0.829_loss0.244_train_auc0.808_loss0.177.pth')
# model = ViT(11, pretrained_backbone=True, mixed_precision=True, model_name='vit_base_patch16_384')
# model = EfficientNetSA(11, pretrained_backbone=True, mixed_precision=True, model_name='tf_efficientnet_b5_ns')
model = Inception(11,
                  pretrained_backbone=False,
                  mixed_precision=False,
                  model_name='inception_v3',
                  checkpoint_path='inception_v3_chestx.pth')

scaler = GradScaler()
if torch.cuda.device_count() > 1:
    model = torch.nn.DataParallel(model)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# class_weights = [354.625, 23.73913043478261, 2.777105767812362, 110.32608695652173,
#                  52.679245283018865, 9.152656621728786, 4.7851333032083145,
#                  8.437891632878731, 2.4620064899945917, 0.4034751151063363, 31.534942820838626]
wandb.config.is_loss_weights = 'no'
class_names = [
    'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', 'NGT - Abnormal',
    'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',