示例#1
0
def predict_csv(model, filename_csv, image_shape,
                model_convert_gpu=True, batch_size=64, argmax=True):

    assert os.path.exists(filename_csv), "csv file not exist"

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if model_convert_gpu and torch.cuda.device_count() > 0:
        model.to(device)
    is_inception = is_inception_model(model)
    if model_convert_gpu and torch.cuda.device_count() > 1 and (not is_inception):
        model = nn.DataParallel(model)
    model.eval()

    dataset = Dataset_CSV(csv_file=filename_csv, image_shape=image_shape, test_mode=True)
    data_loader = DataLoader(dataset, batch_size=batch_size,
                             num_workers=4)
    list_probs = []
    with torch.no_grad():
        for batch_idx, inputs in enumerate(data_loader):
            print('batch:', batch_idx)
            inputs = inputs.to(device)
            outputs = model(inputs)
            probabilities = F.softmax(outputs, dim=1).data
            list_probs.append(probabilities.cpu().numpy())

    probs = np.vstack(list_probs)

    if argmax:
        y_preds = probs.argmax(axis=-1)
        return probs, y_preds
    else:
        return probs
示例#2
0
def predict_csv_single_model(model,
                             filename_csv,
                             image_shape,
                             activation,
                             batch_size=64):

    # assert os.path.exists(filename_csv), "csv file not exist"
    assert filename_csv.exists(), "csv file not exist"

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if torch.cuda.device_count() > 0:
        model.to(device)
    if torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)
    model.eval()

    dataset = Dataset_CSV(data_source=filename_csv,
                          image_shape=image_shape,
                          test_mode=True)
    data_loader = DataLoader(dataset, batch_size=batch_size, num_workers=4)
    list_probs = []
    for batch_idx, inputs in enumerate(data_loader):
        print('batch:', batch_idx)
        inputs = inputs.to(device)
        outputs = model(inputs)
        if activation == 'sigmoid':
            outputs = torch.sigmoid(outputs)
        # if activation == 'softmax':
        #     outputs = torch.softmax(outputs, dim=1)
        # probabilities = F.softmax(outputs, dim=1).data
        list_probs.append(outputs.cpu().numpy())

    probs = np.vstack(list_probs)
    '''release memory It may not be necessary.
    del model
    if torch.cuda.device_count() > 0:
        torch.cuda.empty_cache()
    '''

    return probs
示例#3
0
def gen_baselines(csv_file, label, image_shape, sample_size):

    df = pd.read_csv(csv_file)
    df = df[df['labels'] == label]
    assert len(
        df
    ) > sample_size, 'the length of df should great then the sample size.'
    df = sklearn.utils.shuffle(df, random_state=22222)

    # add_black_interval = 16

    tensor_shape = list(image_shape)
    tensor_shape.insert(0, 3)
    tensor_shape.insert(0, 1)
    # tensor_shape = tuple(tensor_shape)
    # tensor_shape = (1, 3, 299, 299)

    # img_black = np.zeros(tensor_shape)

    ds = Dataset_CSV(data_source=df,
                     multi_labels=True,
                     image_shape=image_shape)

    for i in range(sample_size):
        data1 = ds[i][0].cpu().numpy()
        data1 = np.expand_dims(data1, axis=0)

        # if (i % add_black_interval == 0):
        #     data1 = np.concatenate((data1, img_black), axis=0)

        if 'data_x' not in locals().keys():
            data_x = data1
        else:
            data_x = np.concatenate((data_x, data1), axis=0)

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

    return data_x
# 'xception', 'inception_resnet_v2', 'inception_v3'
# for model_name in ['resnest50d', 'resnest101e', 'res2net50_26w_6s', 'tf_efficientnet_b2', 'tf_efficientnet_b3', 'xception', 'inception_resnet_v2', 'inception_v3']:
# for model_name in ['tf_efficientnet_b2', 'tf_efficientnet_b3', 'xception', 'inception_v3', 'inception_resnet_v2', 'resnest50d_4s2x40d', 'resnest101e', 'res2net50_26w_6s']:
for model_name in ['inception_resnet_v2', 'xception', 'inception_v3']:
    model, image_shape = load_model(model_name,
                                    num_class=num_class,
                                    get_image_shape=True)

    loss_pos_weights = torch.FloatTensor(positive_weights)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if torch.cuda.device_count() > 0:
        model.to(device)
        loss_pos_weights = loss_pos_weights.cuda()

    ds_train = Dataset_CSV(data_source=csv_train,
                           imgaug_iaa=iaa,
                           image_shape=image_shape)
    loader_train = DataLoader(ds_train,
                              batch_size=batch_size_train,
                              shuffle=True,
                              num_workers=num_workers)
    ds_valid = Dataset_CSV(data_source=csv_valid, image_shape=image_shape)
    loader_valid = DataLoader(ds_valid,
                              batch_size=batch_size_valid,
                              num_workers=num_workers)
    ds_test = Dataset_CSV(data_source=csv_test, image_shape=image_shape)
    loader_test = DataLoader(ds_test,
                             batch_size=batch_size_valid,
                             num_workers=num_workers)

    criterion = nn.BCEWithLogitsLoss(pos_weight=loss_pos_weights,
示例#5
0
                                    num_class=num_classes,
                                    model_file=model_file)
    layer_features = model.global_pool
    batch_size = 32

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.eval()
if torch.cuda.device_count() > 0:
    model.to(device)
if torch.cuda.device_count() > 1:
    model = nn.DataParallel(model)

#endregion

ds_test = Dataset_CSV(data_source=csv_file,
                      image_shape=image_shape,
                      test_mode=True)
loader_test = DataLoader(ds_test, batch_size=batch_size, num_workers=4)

features = compute_features_files(model, layer_features, loader_test)

X_tsne = gen_tse_features(features)

if save_features:
    npy_file_features = "/disk1/share_8tb/广角眼底2021.04.12/results/T-SNE/test"
    os.makedirs(os.path.dirname(npy_file_features), exist_ok=True)
    import numpy as np
    np.save(npy_file_features, X_tsne)  # X_tsne = np.load(save_npy_file)

for i in range(num_classes):
    tsne_image_file = f'/disk1/share_8tb/广角眼底2021.04.22/results/T-SNE/test/{i}_{model_name}.png'
示例#6
0
sys.path.append(os.path.abspath('../'))
from libs.dataset.my_dataset import Dataset_CSV
from imgaug import augmenters as iaa

print(os.path.abspath('.'))

csv_file_test = os.path.join(os.path.abspath('.'), 'test.csv')

iaa = iaa.Sequential([
    iaa.flip.Fliplr(p=0.5),
    iaa.flip.Flipud(p=0.5),
    iaa.GaussianBlur(sigma=(0.0, 0.1)),
    iaa.MultiplyBrightness(mul=(0.65, 1.35)),
])
custom_ds = Dataset_CSV(data_source=csv_file_test,
                        imgaug_iaa=iaa,
                        image_shape=[299, 299])
# custom_dl = DataLoader(custom_ds, batch_size=1,
#                        num_workers=1, pin_memory=True)

image_tensor, label = custom_ds[2]
image_np = image_tensor.numpy() * 255
image_np = image_np.astype('uint8')
image_np = image_np.transpose(1, 2, 0)

import cv2
cv2.imwrite('/tmp6/abcdefg.jpg', image_np)
print('OK')

exit(0)
示例#7
0
df = pd.read_csv(filename_train)
num_class = df['labels'].nunique(dropna=True)

# list_class_samples = []
# for label in range(num_class):
#     list_class_samples.append(len(df[df['labels'] == label]))
# sample_class_weights = 1 / np.power(list_class_samples, 0.5)

# sample_class_weights = [1, 3]
# sample_weights = []
# for label in df['labels']:
#     sample_weights.append(sample_class_weights[label])
# sampler = WeightedRandomSampler(weights=sample_weights, num_samples=len(df))

ds_train = Dataset_CSV(csv_file=filename_train,
                       transform=transform_train,
                       image_shape=image_shape)
loader_train = DataLoader(ds_train,
                          batch_size=batch_size_train,
                          shuffle=True,
                          num_workers=8,
                          pin_memory=True)
ds_valid = Dataset_CSV(csv_file=filename_valid, image_shape=image_shape)
loader_valid = DataLoader(ds_valid,
                          batch_size=batch_size_valid,
                          num_workers=8,
                          pin_memory=True)
ds_test = Dataset_CSV(csv_file=filename_test, image_shape=image_shape)
loader_test = DataLoader(ds_test,
                         batch_size=batch_size_valid,
                         num_workers=8,
示例#8
0
# region training
# 'tf_efficientnet_b0', 'res2net50_26w_4s', 'resnet50d' , 'resnest50d'
# 'xception', 'inception_resnet_v2', 'inception_v3'
# for model_name in ['resnest50d', 'resnest101e', 'res2net50_26w_6s', 'tf_efficientnet_b2', 'tf_efficientnet_b3', 'xception', 'inception_resnet_v2', 'inception_v3']:
# for model_name in ['tf_efficientnet_b2', 'tf_efficientnet_b3', 'xception', 'inception_v3', 'inception_resnet_v2', 'resnest50d_4s2x40d', 'resnest101e', 'res2net50_26w_6s']:
for model_name in ['inception_resnet_v2', 'xception', 'inception_v3']:
    model, image_shape = load_model(model_name, num_class=num_class, get_image_shape=True)

    loss_pos_weights = torch.FloatTensor(positive_weights)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if torch.cuda.device_count() > 0:
        model.to(device)
        loss_pos_weights = loss_pos_weights.cuda()

    ds_train = Dataset_CSV(data_source=csv_train, single_label=single_label_no, imgaug_iaa=iaa, image_shape=image_shape)
    loader_train = DataLoader(ds_train, batch_size=batch_size_train, shuffle=True, num_workers=num_workers)
    ds_valid = Dataset_CSV(data_source=csv_valid, single_label=single_label_no, image_shape=image_shape)
    loader_valid = DataLoader(ds_valid, batch_size=batch_size_valid, num_workers=num_workers)
    ds_test = Dataset_CSV(data_source=csv_test, single_label=single_label_no, image_shape=image_shape)
    loader_test = DataLoader(ds_test, batch_size=batch_size_valid, num_workers=num_workers)

    criterion = nn.BCEWithLogitsLoss(pos_weight=loss_pos_weights)
    optimizer = optim.Adam(model.parameters(), weight_decay=0, lr=0.001)
    # from libs.neural_networks.my_optimizer import Lookahead
    # optimizer = Lookahead(optimizer=optimizer, k=5, alpha=0.5)
    scheduler = StepLR(optimizer, step_size=2, gamma=0.3)
    epochs_num = 10

    train(model,
          loader_train=loader_train,