Instructions to use KristofGaming39/Zer0Shot-image-RobloxCharacters with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use KristofGaming39/Zer0Shot-image-RobloxCharacters with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://KristofGaming39/Zer0Shot-image-RobloxCharacters") - Notebooks
- Google Colab
- Kaggle
Download codefortraining.py from KristofGaming39/Zer0Shot-image-RobloxCharacters: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/KristofGaming39/Zer0Shot-image-RobloxCharacters/resolve/main/codefortraining.py
- Command line
-
hf download hf://KristofGaming39/Zer0Shot-image-RobloxCharacters/codefortraining.py
-
curl -L -o codefortraining.py https://huggingface.co/KristofGaming39/Zer0Shot-image-RobloxCharacters/resolve/main/codefortraining.py
2.23 kB
| import torch | |
| import torchvision | |
| from torchvision.models import resnet50 | |
| from torchvision.transforms import transforms | |
| from torch.utils.data import DataLoader, Dataset | |
| # Define the dataset class | |
| class RobloxDataset(Dataset): | |
| def __init__(self, root_dir, transform=None): | |
| self.root_dir = root_dir | |
| self.transform = transform | |
| def __len__(self): | |
| return 200 # Number of images in the dataset, replace with your own to train with your own images. | |
| def __getitem__(self, idx): | |
| img_path = f'{self.root_dir}/human_{str(idx+1).zfill(2)}.png' | |
| image = Image.open(img_path).convert('RGB') | |
| if self.transform: | |
| image = self.transform(image) | |
| return image | |
| data_transform = transforms.Compose([ | |
| transforms.Resize((224, 224)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) | |
| ]) | |
| dataset = RobloxDataset('/content/dataset', transform=data_transform) | |
| data_loader = DataLoader(dataset, batch_size=1, shuffle=True) | |
| model = resnet50(pretrained=True) | |
| model.fc = torch.nn.Linear(in_features=2048, out_features=1) # Adjust the number of output classes if needed | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| model.to(device) | |
| criterion = torch.nn.BCEWithLogitsLoss() | |
| optimizer = torch.optim.Adam(model.parameters(), lr=0.001) | |
| # Train the model | |
| num_epochs = 100 # Adjust the number of training epochs (more epochs = more training time but with more accuracy and less loss). | |
| """ Training the model with more epochs | |
| Pros: | |
| - more accuracy | |
| - less loss (it means the model is improving) | |
| Con: | |
| - more training time | |
| """ | |
| for epoch in range(num_epochs): | |
| for images in data_loader: | |
| images = images.to(device) | |
| labels = torch.ones((images.size(0), 1)).to(device) # Assuming all images belong to the same class | |
| outputs = model(images) | |
| loss = criterion(outputs, labels) | |
| optimizer.zero_grad() | |
| loss.backward() | |
| optimizer.step() | |
| print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item()}') | |
| # Save the trained model | |
| torch.save(model.state_dict(), '/content/zero_shot_classification_model.pth') |