-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
542 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import torch | ||
import torch.nn as nn | ||
import tqdm | ||
from tqdm import tqdm | ||
from forward_process import * | ||
from dataset import * | ||
from dataset import * | ||
import timm | ||
from torch import Tensor, nn | ||
from typing import Callable, List, Tuple, Union | ||
from model import * | ||
from omegaconf import OmegaConf | ||
from sample import * | ||
from visualize import * | ||
|
||
|
||
|
||
def build_model(config): | ||
#model = SimpleUnet() | ||
model = UNetModel(256, 64, dropout=0, n_heads=4 ,in_channels=config.data.imput_channel) | ||
return model | ||
|
||
def fake_real_dataset(config, constants_dict): | ||
train_dataset = Dataset( | ||
root= config.data.data_dir, | ||
category=config.data.category, | ||
config = config, | ||
is_train=True, | ||
) | ||
trainloader = torch.utils.data.DataLoader( | ||
train_dataset, | ||
batch_size=config.data.batch_size, | ||
shuffle=True, | ||
num_workers=config.model.num_workers, | ||
drop_last=True, | ||
) | ||
R_F_dataset=[] | ||
print("Start generating fake real dataset") | ||
for step, batch in tqdm(enumerate(trainloader), total=len(trainloader)): | ||
image = batch[0] | ||
image = image.to(config.model.device) | ||
model = build_model(config) | ||
if config.data.category: | ||
checkpoint = torch.load(os.path.join(os.path.join(os.getcwd(), config.model.checkpoint_dir), config.data.category,'600')) # config.model.checkpoint_name 300+50 | ||
else: | ||
checkpoint = torch.load(os.path.join(os.path.join(os.getcwd(), config.model.checkpoint_dir), '600')) | ||
model.load_state_dict(checkpoint) | ||
model.to(config.model.device) | ||
model.eval() | ||
generate_time_steps = torch.Tensor([config.model.generate_time_steps]).type(torch.int64) | ||
noise = get_noise(image,config) | ||
# noise = forward_diffusion_sample(image, generate_time_steps, constants_dict, config)[0] | ||
seq = range(0, config.model.generate_time_steps, config.model.skip) | ||
H_funcs = Denoising(config.data.imput_channel, config.data.image_size, config.model.device) | ||
reconstructed,_ = efficient_generalized_steps(config, noise, seq, model, constants_dict['betas'], H_funcs, image, cls_fn=None, classes=None) #generalized_steps(noise, seq, model, constants_dict['betas'], config, eta=config.model.eta) | ||
generated_image = reconstructed[-1] | ||
generated_image = generated_image.to(config.model.device) | ||
|
||
for fake, real in zip(generated_image, image): | ||
fake_label = torch.Tensor([1,0]).type(torch.float32).to(config.model.device) | ||
real_label = torch.Tensor([0,1]).type(torch.float32).to(config.model.device) | ||
R_F_dataset.append((fake.type(torch.float32), fake_label)) | ||
R_F_dataset.append((real.type(torch.float32), real_label)) | ||
break | ||
return R_F_dataset | ||
|
||
|
||
|
||
|
||
|
||
|
||
def tune_feature_extractor(constants_dict, config): | ||
R_F_dataset = fake_real_dataset(config, constants_dict) | ||
R_F_dataloader = torch.utils.data.DataLoader(R_F_dataset, batch_size=config.data.batch_size, shuffle=True) | ||
feature_extractor = timm.create_model( | ||
config.model.backbone, | ||
pretrained=True, | ||
num_classes=2, | ||
) | ||
print(feature_extractor.get_classifier()) | ||
num_in_features = feature_extractor.get_classifier().in_features | ||
# feature_extractor.fc = nn.Sequential( | ||
# nn.BatchNorm1d(num_in_features), | ||
# nn.Linear(num_in_features, 512, bias = True), | ||
# nn.ReLU(), | ||
# nn.BatchNorm1d(512), | ||
# nn.Dropout(0.4), | ||
# nn.Linear(512, 2, bias = False), | ||
# ) | ||
feature_extractor.to(config.model.device) | ||
feature_extractor.train() | ||
optimizer = torch.optim.Adam(feature_extractor.parameters(), lr=config.model.learning_rate) | ||
criterion = nn.CrossEntropyLoss() #nn.BCELoss() | ||
print("Start training feature extractor") | ||
if False: | ||
for epoch in tqdm(range(100)): | ||
for step, batch in enumerate(R_F_dataloader): | ||
image = batch[0] | ||
label = batch[1] | ||
# plt.figure(figsize=(11,11)) | ||
# plt.axis('off') | ||
# plt.subplot(1, 1, 1) | ||
# plt.imshow(show_tensor_image(image)) | ||
# plt.title(label[0]) | ||
# plt.savefig('results/F_or_R{}_{}.png'.format(epoch,step)) | ||
# plt.close() | ||
output = feature_extractor(image) | ||
if epoch ==49: | ||
for l, o in zip(label, output): | ||
print('output : ' , o , 'label : ' , l,'\n') | ||
loss = criterion(output, label) | ||
loss.requires_grad = True | ||
optimizer.zero_grad() | ||
|
||
loss.backward() | ||
optimizer.step() | ||
print("Epoch: ", epoch, "Loss: ", loss.item()) | ||
if config.data.category: | ||
torch.save(feature_extractor.state_dict(), os.path.join(os.path.join(os.getcwd(), config.model.checkpoint_dir), config.data.category,'feature')) | ||
else: | ||
torch.save(feature_extractor.state_dict(), os.path.join(os.path.join(os.getcwd(), config.model.checkpoint_dir), 'feature')) | ||
|
||
return feature_extractor | ||
|
||
|
||
|
||
|
||
|
||
def extract_features(feature_extractor, x, out_indices, config): | ||
with torch.no_grad(): | ||
feature_extractor.eval() | ||
reverse_transforms = transforms.Compose([ | ||
transforms.Lambda(lambda t: (t + 1) / (2)), | ||
# transforms.Lambda(lambda t: t * 255.), | ||
# transforms.Lambda(lambda t: t.cpu().numpy().astype(np.uint8)), | ||
# transforms.ToPILImage(), | ||
]) | ||
x = reverse_transforms(x) | ||
|
||
for param in feature_extractor.parameters(): | ||
param.requires_grad = False | ||
feature_extractor.features_only = True | ||
activations = [] | ||
for name, module in feature_extractor.named_children(): | ||
x = module(x) | ||
# print('name : ', name) | ||
if name in ['layer1', 'layer3']: | ||
activations.append(x) | ||
embeddings = activations[0] | ||
for feature in activations[1:]: | ||
layer_embedding = feature | ||
layer_embedding = F.interpolate(layer_embedding, size=int(embeddings.shape[-2]), mode='bilinear', align_corners=False) | ||
embeddings = torch.cat((embeddings,layer_embedding),1) | ||
return embeddings | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.