def ICSTN(opt,imageFull,p): def conv2Layer(opt,feat,outDim): weight,bias = createVariable(opt,[7,7,int(feat.shape[-1]),outDim],stddev=opt.stdGP) conv = tf.nn.conv2d(feat,weight,strides=[1,1,1,1],padding="VALID")+bias return conv def linearLayer(opt,feat,outDim): weight,bias = createVariable(opt,[int(feat.shape[-1]),outDim],stddev=opt.stdGP) fc = tf.matmul(feat,weight)+bias return fc imageWarpAll = [] for l in range(opt.warpN): with tf.variable_scope("geometric",reuse=l>0): pMtrx = warp.vec2mtrx(opt,p) imageWarp = warp.transformCropImage(opt,imageFull,pMtrx) imageWarpAll.append(imageWarp) feat = imageWarp with tf.variable_scope("conv1"): feat = conv2Layer(opt,feat,6) feat = tf.nn.relu(feat) with tf.variable_scope("conv2"): feat = conv2Layer(opt,feat,24) feat = tf.nn.relu(feat) feat = tf.reshape(feat,[opt.batchSize,-1]) with tf.variable_scope("fc3"): feat = linearLayer(opt,feat,opt.warpDim) dp = feat p = warp.compose(opt,p,dp) pMtrx = warp.vec2mtrx(opt,p) imageWarp = warp.transformCropImage(opt,imageFull,pMtrx) imageWarpAll.append(imageWarp) return imageWarpAll
def perturbBG(opt, imageData): """ generate pereturbed image 교란된 이미지 생성하는 함수 :param opt: 옵션 :param imageData: 전체 이미지 데이터셋 :return: 이미지 데이터 """ # batch 사이즈 만큼의 랜덤한 값을 가진 배열들을 생성하여 각 원소에 pertBG 값 곱해줌 rot = opt.pertBG * tf.random_normal([opt.batchSize]) tx = opt.pertBG * tf.random_normal([opt.batchSize]) ty = opt.pertBG * tf.random_normal([opt.batchSize]) # 배치 사이즈 만큼의 0으로만 이루어진 배열 생성 O = tf.zeros([opt.batchSize]) # 교란된 백그라운드 이미지(인물 사진) 생성을 위한 warp 매개변수 생성 # 왜곡 타입이 homography(가로, 세로간의 평행을 유지하지 않고 이미지를 왜곡시킴)일 때와 # 왜곡 타입이 affine(가로, 세로간의 평행을 유지하며 이미지를 왜곡시킴)으로 나누어 생성 pPertBG = \ tf.stack([tx, rot, O, O, ty, -rot, O, O], axis=1) if opt.warpType == "homography" else\ tf.stack([O, rot, tx, -rot, O, ty], axis=1) if opt.warpType == "affine" else None # 벡터 형태의 warp 매개변수를 matrix로 변환 pPertBGmtrx = warp.vec2mtrx(opt, pPertBG) # warp 파라미터에 따라 이미지 데이터셋을 변형 및 크롭하여 생성 image = warp.transformCropImage(opt, imageData, pPertBGmtrx) return image
def perturbBG(opt, imageData): rot = opt.pertBG * tf.random_normal([opt.batchSize]) tx = opt.pertBG * tf.random_normal([opt.batchSize]) ty = opt.pertBG * tf.random_normal([opt.batchSize]) O = tf.zeros([opt.batchSize]) pPertBG = tf.stack([tx,rot,O,O,ty,-rot,O,O],axis=1) if opt.warpType=="homography" else \ tf.stack([O,rot,tx,-rot,O,ty],axis=1) if opt.warpType=="affine" else None pPertBGmtrx = warp.vec2mtrx(opt, pPertBG) image = warp.transformCropImage(opt, imageData, pPertBGmtrx) return image
print(util.toMagenta("building graph...")) tf.reset_default_graph() # build graph with tf.device("/gpu:0"): # ------ define input data ------ imageFull = tf.placeholder(tf.float32,shape=[opt.batchSize,opt.fullH,opt.fullW,3]) imageMean,imageVar = tf.nn.moments(imageFull,axes=[1,2],keep_dims=True) imageFullNormalize = (imageFull-imageMean)/tf.sqrt(imageVar) label = tf.placeholder(tf.int64,shape=[opt.batchSize]) PH = [imageFull,label] # ------ generate perturbation ------ pInit = data.genPerturbations(opt) pInitMtrx = warp.vec2mtrx(opt,pInit) # ------ build network ------ imagePert = warp.transformCropImage(opt,imageFullNormalize,pInitMtrx) imagePertRescale = imagePert*tf.sqrt(imageVar)+imageMean if opt.netType=="CNN": output = graph.fullCNN(opt,imagePert) elif opt.netType=="STN": imageWarpAll = graph.STN(opt,imagePert) imageWarp = imageWarpAll[-1] output = graph.CNN(opt,imageWarp) imageWarpRescale = imageWarp*tf.sqrt(imageVar)+imageMean elif opt.netType=="IC-STN": imageWarpAll = graph.ICSTN(opt,imageFullNormalize,pInit) imageWarp = imageWarpAll[-1] output = graph.CNN(opt,imageWarp) imageWarpRescale = imageWarp*tf.sqrt(imageVar)+imageMean softmax = tf.nn.softmax(output) labelOnehot = tf.one_hot(label,opt.labelN)