In [1]:
%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai.imports import *
# from fastai.transforms import import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
In [2]:
PATH = 'SpaceNet_Roads_Sample/'
In [3]:
dirs = os.listdir(PATH)
dirs.sort()
In [4]:
VEGAS, PARIS, SHANG, KHART = [PATH + directory + '/' for directory in dirs]
In [5]:
model = resnet34(True)
Delete top layers:
In [6]:
model = nn.Sequential(*children(model)[:-2],
nn.Conv2d(512, 1, 3, padding=1), # 1 output bc looking for 1 class
nn.AdaptiveAvgPool2d(1), Flatten(), # 2 layers here (pool & flatn)
nn.LogSoftmax())
Data
In [7]:
vegas_csv_path = VEGAS + 'summaryData/' + 'AOI_2_Vegas_Roads_Sample.csv'
vegas_train_folder = VEGAS + 'RGB-PanSharpen'
In [8]:
VEGAS
Out[8]:
In [9]:
os.listdir('SpaceNet_Roads_Sample/AOI_2_Vegas_Roads_Sample/')
Out[9]:
In [10]:
os.listdir(VEGAS+'RGB-PanSharpen')
Out[10]:
In [23]:
# os.mkdir(VEGAS+'train/')
# os.mkdir(VEGAS+'train/one')
files copied to $VEGAS/train/one in terminal
In [11]:
sz = 224
bs = 64
tfms = tfms_from_model(model, sz, aug_tfms=transforms_top_down, max_zoom=1.1)
data = ImageClassifierData.from_paths(VEGAS, tfms=tfms, bs=bs, trn_name='train',
val_name='train')
In [12]:
learner = ConvLearner.from_model_data(model, data)
In [13]:
learner.freeze_to(-4)
In [14]:
model[-1].trainable, model[-4].trainable
Out[14]:
In [15]:
model[-4]
Out[15]:
In [16]:
learner.fit(0.01, 1)
Out[16]:
In [17]:
learner.fit(0.01, 1, cycle_len=1)
Out[17]:
In [18]:
class SaveFeatures():
features=None
def __init__(self, m): self.hook = m.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output): self.features = to_np(output)
def remove(self): self.hook.remove()
In [19]:
x,y = next(iter(data.val_dl))
x,y = x[None, 1], y[None, 1]
vx = Variable(x.cuda(), requires_grad=True)
In [20]:
dx = data.val_ds.denorm(x)[0]
plt.imshow(35*dx);
In [21]:
sf = SaveFeatures(model[-4])
py = model(Variable(x.cuda()))
sf.remove()
py = np.exp(to_np(py)[0]); py
Out[21]:
In [22]:
feat = np.maximum(0, sf.features[0])
feat.shape
Out[22]:
In [23]:
f2 = np.dot(np.rollaxis(feat, 0, 3), py)
f2 -= f2.min()
f2 /= f2.max()
f2
Out[23]:
In [24]:
plt.imshow(dx*35)
Out[24]:
In [25]:
plt.imshow(dx*35)
plt.imshow(scipy.misc.imresize(f2, dx.shape), alpha=0.5, cmap='hot');
Max -- Coordinate for Road Node Script:
In [29]:
np.argpartition(f2, -4)[-4:]
Out[29]:
In [30]:
np.argmax(f2[0])
Out[30]:
In [31]:
f2[0]
Out[31]:
In [32]:
f2
Out[32]:
In [ ]:
In [ ]:
In [26]:
dx.shape
Out[26]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [49]:
ls $VEGAS/summaryData
In [51]:
df = pd.read_csv(VEGAS + 'summaryData/' + 'AOI_2_Vegas_Roads_Sample.csv')
In [52]:
df.head()
Out[52]:
In [ ]:
data = ImageClassifierData.from_csv(vegas_csv_path, )
In [55]:
ls $VEGAS
In [66]:
ls $VEGAS/RGB-PanSharpen/
In [67]:
ls $VEGAS/MUL/
In [72]:
ls $VEGAS/PAN
In [73]:
# img = cv2.imread(f'{VEGAS}RGB-PanSharpen/RGB-PanSharpen_AOI_2_Vegas_img1521.tif')
# img = cv2.imread(f'{VEGAS}MUL/MUL_AOI_2_Vegas_img794.tif')
img = cv2.imread(f'{VEGAS}PAN/PAN_AOI_2_Vegas_img1454.tif')
plt.imshow(img)
Out[73]:
In [79]:
os.listdir('SpaceNet_Roads_Sample')
Out[79]:
In [78]:
os.listdir('SpaceNet_Roads_Sample/AOI_2_Vegas_Roads_Sample')
Out[78]:
In [84]:
os.listdir(PARIS), os.listdir(PARIS + os.listdir(PARIS)[0])
Out[84]:
In [101]:
img = cv2.imread(f'{PARIS}/MUL/MUL_AOI_3_Paris_img432.tif')
# img *= 256
plt.imshow(img)
In [100]:
img.dtype
Out[100]:
In [107]:
img *= 100; img
Out[107]:
In [108]:
plt.imshow(img)
Out[108]:
In [135]:
img = cv2.imread(f'{PARIS}/MUL/MUL_AOI_3_Paris_img432.tif')
img[:,:,1] *= 100
plt.imshow(img)
Out[135]:
In [129]:
img.shape
Out[129]:
In [136]:
os.listdir(PARIS), os.listdir(PARIS + os.listdir(PARIS)[1])
Out[136]:
In [144]:
os.listdir(PARIS)[1]+'/'+os.listdir(PARIS + os.listdir(PARIS)[1])[0]
Out[144]:
In [165]:
# img = cv2.imread(f'{PARIS}/RGB-PanSharpen/RGB-PanSharpen_AOI_3_Paris_img84.tif')
img = cv2.imread(f'{PARIS}/RGB-PanSharpen/RGB-PanSharpen_AOI_3_Paris_img406.tif')
# img *= 80
# for i,e in enumerate(img):
# img[i] = 255 * e / img.max()
plt.imshow(img)
Out[165]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: