Example #1
0
    # 轉灰階: 將輸入3維壓成1維。
    transforms.Grayscale(),
    # 縮放: 因為source data是32x32,我們將target data的28x28放大成32x32。
    transforms.Resize((32, 32)),
    # 水平翻轉 (Augmentation)
    transforms.RandomHorizontalFlip(),
    # 旋轉15度內 (Augmentation),旋轉後空的地方補0
    transforms.RandomRotation(15),
    # 最後轉成Tensor供model使用。
    transforms.ToTensor(),
])

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
feature_extractor = CNN_VAE().to(device)
#feature_extractor = FeatureExtractor().cuda()
label_predictor = LabelPredictor().to(device)
feature_extractor.load_state_dict(torch.load('extractor_model.bin'))
#label_predictor.load_state_dict(torch.load('predictor_model.bin'))

source_dataset = ImageFolder('real_or_drawing/train_data',
                             transform=source_transform)
target_dataset = ImageFolder('real_or_drawing/test_data',
                             transform=target_transform)
test_dataloader = DataLoader(target_dataset, batch_size=128, shuffle=False)

source_dataloader = DataLoader(source_dataset, batch_size=128, shuffle=False)

label_predictor.eval()
feature_extractor.eval()
for i, ((source_data, source_label),
        (test_data, _)) in enumerate(zip(source_dataloader, test_dataloader)):
Example #2
0
# total_user_data = []
# total_user_labels = []
# for subject_index in range(11):
#     total_user_data.extend(list_total_user_data[subject_index])
#     total_user_labels.extend(list_total_user_labels[subject_index])

total_user_data = np.array(list_total_user_data,
                           dtype=np.float32)  # <np.ndarray> (11, 180, 400, 8)
total_user_labels = np.array(list_total_user_labels,
                             dtype=np.int64)  # <np.ndarray> (11, 180)

# ----------------------------------------- Init Network -------------------------------------------------------- #

feature_extractor = FeatureExtractor().cuda()
domain_classifier = DomainClassifier().cuda()
label_predictor = LabelPredictor().cuda()

feature_extractor.load_state_dict(
    torch.load(r'saved_model\feature_extractor_CE_8_subjects.pkl'))
domain_classifier.load_state_dict(
    torch.load(r'saved_model\domain_classifier_CE_8_subjects.pkl'))
label_predictor.load_state_dict(
    torch.load(r'saved_model\label_predictor_CE_8_subjects.pkl'))

# ------------------------------------------ Testing Stage -------------------------------------------------------- #

window_size = 52
stride = 1
max_fit = 30
jump = 1
threshold = 60 / 128
Example #3
0
# tensor to DataLoader
train_dataloader = DataLoader(train_data,
                              batch_size=1024,
                              shuffle=True,
                              drop_last=True)
test_dataloader = DataLoader(test_data,
                             batch_size=1024,
                             shuffle=True,
                             drop_last=True)

# -------------------------------------- Training Stage ------------------------------------------- #

precision = 1e-8

feature_extractor = FeatureExtractor().cuda()
label_predictor = LabelPredictor().cuda()
domain_classifier = DomainClassifier().cuda()

class_criterion = nn.CrossEntropyLoss()
domain_criterion = nn.CrossEntropyLoss()

optimizer_F = optim.Adam(feature_extractor.parameters())
optimizer_C = optim.Adam(label_predictor.parameters())
optimizer_D = optim.Adam(label_predictor.parameters())

scheduler_F = optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer_F,
                                                   mode='min',
                                                   factor=0.1,
                                                   patience=8,
                                                   verbose=True,
                                                   eps=precision)
Example #4
0
                            target_transform,
                            './real_or_drawing',
                            train=True)
print('dataset done')
target_dataset = ImgDataset(source_transform,
                            target_transform,
                            './real_or_drawing',
                            train=False)
print('dataset done 2')

source_dataloader = DataLoader(source_dataset, batch_size=32, shuffle=True)
target_dataloader = DataLoader(target_dataset, batch_size=32, shuffle=True)
test_dataloader = DataLoader(target_dataset, batch_size=128, shuffle=False)

feature_extractor = FeatureExtractor().cuda()
label_predictor = LabelPredictor().cuda()
domain_classifier = DomainClassifier().cuda()

class_criterion = nn.CrossEntropyLoss()
domain_criterion = nn.BCEWithLogitsLoss()

optimizer_F = optim.Adam(feature_extractor.parameters())  #原為1e-3
optimizer_C = optim.Adam(label_predictor.parameters())
optimizer_D = optim.Adam(domain_classifier.parameters())


def train_epoch(source_dataloader, target_dataloader, lamb):
    '''
      Args:
        source_dataloader: source data的dataloader
        target_dataloader: target data的dataloader
Example #5
0
    # 轉灰階: 將輸入3維壓成1維。
    transforms.Grayscale(),
    # 縮放: 因為source data是32x32,我們將target data的28x28放大成32x32。
    transforms.Resize((32, 32)),
    # 水平翻轉 (Augmentation)
    transforms.RandomHorizontalFlip(),
    # 旋轉15度內 (Augmentation),旋轉後空的地方補0
    transforms.RandomRotation(15),
    # 最後轉成Tensor供model使用。
    transforms.ToTensor(),
])

set_seed(208)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
feature_extractor = CNN_VAE().to(device)
label_predictor = LabelPredictor().to(device)
domain_classifier = DomainClassifier().to(device)

class_criterion = nn.CrossEntropyLoss()
domain_criterion = nn.BCEWithLogitsLoss()

source_dataset = ImageFolder('real_or_drawing/train_data',
                             transform=source_transform)
target_dataset = ImageFolder('real_or_drawing/test_data',
                             transform=target_transform)

source_dataloader = DataLoader(source_dataset, batch_size=32, shuffle=True)
target_dataloader = DataLoader(target_dataset, batch_size=32, shuffle=True)
test_dataloader = DataLoader(target_dataset, batch_size=128, shuffle=False)

train(source_dataloader,
Example #6
0
    # 轉灰階: 將輸入3維壓成1維。
    transforms.Grayscale(),
    # 縮放: 因為source data是32x32,我們將target data的28x28放大成32x32。
    transforms.Resize((32, 32)),
    # 水平翻轉 (Augmentation)
    #transforms.RandomHorizontalFlip(),
    # 旋轉15度內 (Augmentation),旋轉後空的地方補0
    #transforms.RandomRotation(15),
    # 最後轉成Tensor供model使用。
    transforms.ToTensor(),
])

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
feature_extractor = CNN_VAE().to(device)
#feature_extractor = FeatureExtractor().cuda()
label_predictor = LabelPredictor().to(device)
feature_extractor.load_state_dict(torch.load('extractor_model.bin'))
label_predictor.load_state_dict(torch.load('predictor_model.bin'))

target_dataset = ImageFolder(os.path.join(sys.argv[1], 'test_data'),
                             transform=target_transform)
test_dataloader = DataLoader(target_dataset, batch_size=128, shuffle=False)

result = []
label_predictor.eval()
feature_extractor.eval()
for i, (test_data, _) in enumerate(test_dataloader):
    test_data = test_data.to(device)

    class_logits = label_predictor(feature_extractor(test_data))
Example #7
0
    # 縮放: 因為source data是32x32,我們將target data的28x28放大成32x32。
    transforms.Resize((32, 32)),
    # 水平翻轉 (Augmentation)
    transforms.RandomHorizontalFlip(),
    # 旋轉15度內 (Augmentation),旋轉後空的地方補0
    transforms.RandomRotation(15),#, fill=(0,)),
    # 最後轉成Tensor供model使用。
    transforms.ToTensor(),
])

target_dataset = ImageFolder('real_or_drawing/test_data', transform=target_transform)
test_dataloader = DataLoader(target_dataset, batch_size=128, shuffle=False)

feature_extractor = FeatureExtractor().cuda()
feature_extractor.load_state_dict(torch.load('strong1_extractor_model_1000.bin'))
label_predictor = LabelPredictor().cuda()
label_predictor.load_state_dict(torch.load('strong1_predictor_model_1000.bin'))
domain_classifier = DomainClassifier().cuda()
#domain_classifier.load_state_dict(torch.load('extractor_model_300.bin'))

feature_extractor.eval()
label_predictor.eval()
label_dict = {}
for i in range(10):
    label_dict[i] = []

for i, (test_data, _) in enumerate(test_dataloader):
    test_data = test_data.cuda()
    class_logits = label_predictor(feature_extractor(test_data))
    x = torch.argmax(class_logits, dim=1).cpu().detach().numpy()
    _y = torch.max(class_logits, dim=1)