U ͐ib@shdZddlZddlmZddlmZdddZGdd d ejZGd d d ejZGd d d ejZ dS)z Originally developed by XD_XD in the SpaceNet 4 Challenge , then integrated by Solaris project. We modified the code to accomodate different number of input channels, e.g. 5-channel RGB+LIDAR input images. N)nn)vgg16csGfdddt}|S)a Get a modified VGG16-Unet model with customized input channel numbers. For example, we can set in_channels=3 and input RGB 3-channel images. On the other hand, we can set in_channels=5 if we want to input both RGB and 2-channel LIDAR data (elevation + intensity). cseZdZfddZZS)z3get_modified_vgg16_unet..Modified_VGG16UnetcstjddS)N in_channels)super__init__)self) __class__rd/home/ec2-user/SageMaker/vegetation-management-remars2022/remars2022-workshop/networks/vgg16_unet.pyrsz.Modified_VGG16Unet.__init__)__name__ __module__ __qualname__r __classcell__r rr r Modified_VGG16Unetsr) VGG16Unet)rrr rr get_modified_vgg16_unetsrcs$eZdZfddZddZZS) DecoderBlockcs<t||_ttjddddt||t|||_dS)NbilinearF) scale_factormode align_corners)rrrr SequentialUpsampleConvRelublock)r rmiddle_channels out_channelsrr r rs zDecoderBlock.__init__cCs ||SN)rr xr r r forward$szDecoderBlock.forwardr rrrr$rr r rr rs rcs$eZdZfddZddZZS)rcs0ttj||ddd|_tjdd|_dS)Nr)paddingTinplace)rrrConv2dconvReLU activation)r in_outrr r r(s zConvRelu.__init__cCs||}||}|Sr!)r+r-r"r r r r$-s  zConvRelu.forwardr%r r rr r's rcs&eZdZdfdd ZddZZS) rr Fcstt|dj|_tdd|_tjdd|_ tj |ddddd|jd <t |jd |j |jd|j |_ t |jd |j |jd |j |_ t |jd |j |jd |j |jd|j |_t |jd|j |jd|j |jd|j |_t |jd|j |jd|j |jd|j |_td|dd|d|_td|d|dd|d|_td|d|dd|d|_td|d|dd|d|_td|d|dd||_td|||_tj |ddd|_dS)N) pretrainedrTr(@rr&) kernel_sizestrider'r i)r3)rrrfeaturesencoderr MaxPool2dpoolr,relur*rconv1conv2conv3conv4conv5rcenterdec5dec4dec3dec2rdec1final)r r num_filtersr1rr r r3s       zVGG16Unet.__init__cCs||}|||}|||}|||}|||}|||}|t ||gd}| t ||gd} | t | |gd} | t | |gd} | t | |gd} || } | S)Nr&)rIrJrGrKrLrMrNrOtorchcatrPrQrRrSrT)r r#rIrJrKrLrMrNrOrPrQrRrSx_outr r r r$[s  zVGG16Unet.forward)rr0Fr%r r rr r2s(r)r) __doc__rVrZtorchvision.modelsrrModulerrrr r r r s