init
This commit is contained in:
@@ -1,21 +1,45 @@
|
||||
# tensor - numpy - PILImage 변환 (https://qlsenddl-lab.tistory.com/37)
|
||||
|
||||
import os
|
||||
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
||||
from datasets import load_dataset
|
||||
from datasets import Dataset
|
||||
import torch
|
||||
import torchvision.transforms as transforms
|
||||
|
||||
# load cifar10 (only small portion for demonstration purposes)
|
||||
train_ds, test_ds = load_dataset('cifar10', split=['train[:5000]', 'test[:2000]'])
|
||||
# split up training into training + validation
|
||||
splits = train_ds.train_test_split(test_size=0.1)
|
||||
train_ds = splits['train']
|
||||
val_ds = splits['test']
|
||||
from stock.util.Stock2Vector import Stock2Vector
|
||||
|
||||
id2label = {id:label for id, label in enumerate(train_ds.features['label'].names)}
|
||||
label2id = {label:id for id,label in id2label.items()}
|
||||
PROJECT_HOME = os.path.join(os.path.dirname(__file__))
|
||||
RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources")
|
||||
stock2Vector = Stock2Vector(RESOURCE_PATH)
|
||||
X, Y = stock2Vector.getDataset2D("252670")
|
||||
|
||||
trans = transforms.ToPILImage()
|
||||
X = [trans(torch.tensor([x])) for x in X]
|
||||
|
||||
split_point1 = int(len(X)*0.7)
|
||||
split_point2 = int(len(X)*0.9)
|
||||
train_X = X[:split_point1]
|
||||
train_Y = Y[:split_point1]
|
||||
valid_X = X[split_point1:split_point2]
|
||||
valid_Y = X[split_point1:split_point2]
|
||||
test_X = X[split_point2:]
|
||||
test_Y = X[split_point2:]
|
||||
|
||||
id2label = {0: '0', 1: '1', 2: '2'}
|
||||
label2id = {'0': 0, '1': 1, '2': 2}
|
||||
|
||||
# load cifar10 (only small portion for demonstration purposes)
|
||||
train_data = {'img': train_X, 'label': train_Y}
|
||||
val_dsta = {'img': valid_X, 'label': valid_Y}
|
||||
test_data = {'img': test_X, 'label': test_Y}
|
||||
|
||||
train_ds = Dataset.from_dict(train_data)
|
||||
val_ds = Dataset.from_dict(val_dsta)
|
||||
test_ds = Dataset.from_dict(test_data)
|
||||
|
||||
from transformers import ViTFeatureExtractor
|
||||
|
||||
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
|
||||
feature_extractor = ViTFeatureExtractor()
|
||||
|
||||
from torchvision.transforms import (CenterCrop,
|
||||
Compose,
|
||||
@@ -67,7 +91,10 @@ def collate_fn(examples):
|
||||
return {"pixel_values": pixel_values, "labels": labels}
|
||||
|
||||
train_dataloader = DataLoader(train_ds, collate_fn=collate_fn, batch_size=4)
|
||||
|
||||
train_data_loader = torch.utils.data.DataLoader(train_X,
|
||||
batch_size=32,
|
||||
shuffle=True,
|
||||
num_workers=16)
|
||||
|
||||
batch = next(iter(train_dataloader))
|
||||
for k,v in batch.items():
|
||||
|
||||
Reference in New Issue
Block a user