from model import FineTunedModel from torchvision.models import resnet18 from model import ContrastiveModel, get_backbone from dataset_loader import get_clf_train_test_transform from dataset_loader import get_clf_train_test_dataloaders if torch.cuda.is_available(): dtype = torch.cuda.FloatTensor device = torch.device("cuda") # torch.cuda.set_device(device_id) print('GPU') else: dtype = torch.FloatTensor device = torch.device("cpu") backbone = get_backbone(resnet18(pretrained=False)) model = ContrastiveModel(backbone).to(device) obj = torch.load( "/home/octo/Desktop/clr/ckpt/0417151425/SimCLR_0417175433.pth") model.load_state_dict(obj['state_dict']) encoder = model.backbone last_layers = torch.nn.Sequential( *(list(model.projectionhead.children())[0:2])) encoder = nn.Sequential(encoder, last_layers) new_model = FineTunedModel(encoder, model.output_dim).to(device) criterion = nn.CrossEntropyLoss()
from model import RetinaNet, get_backbone from loss import RetinaNetLoss from dataloader import FaceMask from box import LabelEncoder num_classes = 2 classes_name = ['face', 'mask'] batch_size = 2 label_encoder = LabelEncoder() learning_rates = [2.5e-06, 0.000625, 0.00125, 0.0025, 0.00025, 2.5e-05] learning_rate_boundaries = [125, 250, 500, 240000, 360000] learning_rate_fn = tf.optimizers.schedules.PiecewiseConstantDecay( boundaries=learning_rate_boundaries, values=learning_rates) resnet50_backbone = get_backbone() loss_fn = RetinaNetLoss(num_classes) model = RetinaNet(num_classes, resnet50_backbone) optimizer = tf.optimizers.SGD(learning_rate=learning_rate_fn, momentum=0.9) model.compile(loss=loss_fn, optimizer=optimizer) train_img_paths = sorted(glob('./facedataset/images/train/*.jpg')) train_labels_paths = sorted(glob('./facedataset/labels/train/*.txt')) val_img_paths = sorted(glob('./facedataset/images/val/*.jpg')) val_labels_paths = sorted(glob('./facedataset/labels/val/*.txt')) train_gen = FaceMask(train_img_paths, train_labels_paths) train_dataset = tf.data.Dataset.from_generator( lambda: train_gen, output_types=(tf.float32, tf.float32, tf.int32),