forked from ermongroup/tile2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_1.py
147 lines (119 loc) · 4.15 KB
/
table_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
import os
import torch
from glob import glob
from torch import optim
from time import time
import numpy as np
from scipy import stats
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
tile2vec_dir = '/home/users/ebarnett/tile2vec'
sys.path.append('../')
sys.path.append(tile2vec_dir)
from src.datasets import TileTripletsDataset, GetBands, RandomFlipAndRotate, ClipAndScale, ToFloatTensor, triplet_dataloader
from src.tilenet import make_tilenet
from src.training import prep_triplets, train_triplet_epoch
from src.resnet import ResNet18
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader, TensorDataset
class NAIP(Dataset):
def __init__(self, paths):
self.paths = []
for p in paths:
try:
a = np.load(p)
c, w, h = a.shape
if w and h >= 50:
self.paths.append(p)
except:
continue
def __len__(self):
return len(self.paths)
def __getitem__(self, index):
img = np.load(self.paths[index])
c, w, h = img.shape
img = img[:,int(w/2)-25:int(w/2)+25, int(h/2)-25:int(h/2)+25]
y = stats.mode(img[4,:,:])[0][0][0]
img = img[0:4,:,:]
return img, y
model_dir = '/home/ebarnett/tile2vec/models/'
tile_dir = '/raid/users/ebarnett/tile2vec/tiles/'
# Environment stuff
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
cuda = torch.cuda.is_available()
# USING PRETRAINED MODEL
print("Using pretrained model")
# Setting up model
in_channels = 4
z_dim = 512
n_trials = 5
cuda = torch.cuda.is_available()
# tilenet = make_tilenet(in_channels=in_channels, z_dim=z_dim)
# Use old model for now
tilenet = ResNet18()
if cuda: tilenet.cuda()
# GET DATA
states_dir = os.path.join('/home/bjohnson/projects/naip_collect/data/states')
states_paths = [i for i, f, r in os.walk(states_dir)][1::]
patches_paths = []
states_paths = states_paths[0:5]
for state in states_paths:
for _, _, files in os.walk(state):
for f in files:
if f.endswith(".npy"):
patches_paths.append(os.path.join(state, f))
print(len(patches_paths))
# Initialized DataLoader
dataset = NAIP(patches_paths)
dataloader = DataLoader(dataset,
batch_size=64,
shuffle=True,
num_workers=4)
print("done with dataloader")
# Load parameters
model_fn = os.path.join(model_dir, 'naip_trained.ckpt')
checkpoint = torch.load(model_fn)
tilenet.load_state_dict(checkpoint)
tilenet.eval()
print("training...")
y = []
X = []
for idx, (tile, lab) in enumerate(dataloader):
tile = torch.squeeze(tile.float())
tile = Variable(tile)
if cuda: tile = tile.cuda()
z = tilenet.encode(tile)
if cuda: z = z.cpu()
X.append(z.data.numpy())
y.append(lab.data.numpy())
t1 = time()
X = np.concatenate(X, axis=0)
y = np.concatenate(y, axis=0)
for n_tiles in [1000, 10000]:
# Embed tiles
y = LabelEncoder().fit_transform(y)
accs_rf = np.zeros((n_trials,))
accs_log = np.zeros((n_trials,))
accs_nn = np.zeros((n_trials,))
for i in range(n_trials):
# Splitting data and training RF classifer
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=X.shape[0]-n_tiles)
rf = RandomForestClassifier(n_estimators=1000)
rf.fit(X_tr, y_tr)
accs_rf[i] = rf.score(X_te, y_te)
# log = LogisticRegression(max_iter=1000)
# log.fit(X_tr, y_tr)
# accs_log[i] = log.score(X_te, y_te)
# nn = MLPClassifier()
# nn.fit(X_tr, y_tr)
# accs_nn[i] = nn.score(X_te, y_te)
print('RF Mean accuracy: {:0.4f}'.format(accs_rf.mean()))
print('RF Standard deviation: {:0.4f}'.format(accs_rf.std()))
print('LOG Mean accuracy: {:0.4f}'.format(accs_log.mean()))
print('LOG Standard deviation: {:0.4f}'.format(accs_log.std()))
print('nn Mean accuracy: {:0.4f}'.format(accs_nn.mean()))
print('nn Standard deviation: {:0.4f}'.format(accs_nn.std()))