MAE_ATMO/torch_MAE_1d_decoder.ipynb

58 KiB
Raw Permalink Blame History

In [1]:
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset, random_split
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pandas as pd
In [2]:
np.random.seed(0)
torch.random.manual_seed(0)
Out[2]:
<torch._C.Generator at 0x7fb6e75377f0>
In [3]:
max_pixel_value = 107.49169921875
In [4]:
class NO2Dataset(Dataset):
    
    def __init__(self, image_dir, mask_dir):
        
        self.image_dir = image_dir
        self.mask_dir = mask_dir
        self.image_filenames = [f for f in os.listdir(image_dir) if f.endswith('.npy')]  # 仅加载 .npy 文件
        self.mask_filenames = [f for f in os.listdir(mask_dir) if f.endswith('.jpg')]  # 仅加载 .jpg 文件
        
    def __len__(self):
        
        return len(self.image_filenames)
    
    def __getitem__(self, idx):
        
        image_path = os.path.join(self.image_dir, self.image_filenames[idx])
        mask_idx = np.random.choice(self.mask_filenames)
        mask_path = os.path.join(self.mask_dir, mask_idx)

        # 加载图像数据 (.npy 文件)
        image = np.load(image_path).astype(np.float32)[:,:,:1] / max_pixel_value  # 形状为 (96, 96, 1)

        # 加载掩码数据 (.jpg 文件)
        mask = np.array(Image.open(mask_path).convert('L')).astype(np.float32)

        # 将掩码数据中非0值设为10值保持不变
        mask = np.where(mask != 0, 1.0, 0.0)

        # 保持掩码数据形状为 (96, 96, 1)
        mask = mask[:, :, np.newaxis]  # 将形状调整为 (96, 96, 1)

        # 应用掩码
        masked_image = image.copy()
        masked_image[:, :, 0] = image[:, :, 0] * mask.squeeze()  # 遮盖NO2数据

        # cGAN的输入和目标
        X = masked_image[:, :, :1]  # 形状为 (96, 96, 8)
        y = image[:, :, 0:1]  # 目标输出为NO2数据形状为 (96, 96, 1)

        # 转换形状为 (channels, height, width)
        X = np.transpose(X, (2, 0, 1))  # 转换为 (1, 96, 96)
        y = np.transpose(y, (2, 0, 1))  # 转换为 (1, 96, 96)
        mask = np.transpose(mask, (2, 0, 1))  # 转换为 (1, 96, 96)

        return torch.tensor(X, dtype=torch.float32), torch.tensor(y, dtype=torch.float32), torch.tensor(mask, dtype=torch.float32)

# 实例化数据集和数据加载器
image_dir = './out_mat/96/train/'
mask_dir = './out_mat/96/mask/20/'
In [5]:
train_set = NO2Dataset(image_dir, mask_dir)
train_loader = DataLoader(train_set, batch_size=64, shuffle=True, num_workers=8)
val_set = NO2Dataset('./out_mat/96/valid/', mask_dir)
val_loader = DataLoader(val_set, batch_size=64, shuffle=False, num_workers=4)
test_set = NO2Dataset('./out_mat/96/test/', mask_dir)
test_loader = DataLoader(test_set, batch_size=64, shuffle=False, num_workers=4)
In [6]:
# 可视化特定特征的函数
def visualize_feature(input_feature,masked_feature, output_feature, title):
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 3, 1)
    plt.imshow(input_feature[0].cpu().numpy(), cmap='RdYlGn_r')
    plt.title(title + " Input")
    plt.subplot(1, 3, 2)
    plt.imshow(masked_feature[0].cpu().numpy(), cmap='RdYlGn_r')
    plt.title(title + " Masked")
    plt.subplot(1, 3, 3)
    plt.imshow(output_feature[0].detach().cpu().numpy(), cmap='RdYlGn_r')
    plt.title(title + " Recovery")
    plt.show()
In [7]:
class Conv(nn.Sequential):
    def __init__(self, in_channels, out_channels, kernel_size=3, dilation=1, stride=1, bias=False):
        super(Conv, self).__init__(
            nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, bias=bias,
                      dilation=dilation, stride=stride, padding=((stride - 1) + dilation * (kernel_size - 1)) // 2)
        )
In [8]:
class ConvBNReLU(nn.Sequential):
    def __init__(self, in_channels, out_channels, kernel_size=3, dilation=1, stride=1, norm_layer=nn.BatchNorm2d,
                 bias=False):
        super(ConvBNReLU, self).__init__(
            nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, bias=bias,
                      dilation=dilation, stride=stride, padding=((stride - 1) + dilation * (kernel_size - 1)) // 2),
            norm_layer(out_channels),
            nn.ReLU()
        )
In [9]:
class SeparableBNReLU(nn.Sequential):
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, dilation=1, norm_layer=nn.BatchNorm2d):
        super(SeparableBNReLU, self).__init__(
            nn.Conv2d(in_channels, in_channels, kernel_size=kernel_size, stride=stride, dilation=dilation,
                      padding=((stride - 1) + dilation * (kernel_size - 1)) // 2, groups=in_channels, bias=False),
            # 分离卷积,仅调整空间信息
            norm_layer(in_channels),  # 对输入通道进行归一化
            nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False),  # 这里进行升维操作
            nn.ReLU6()
        )
In [10]:
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1, downsample=None):
        super(ResidualBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channels)

        # 如果输入和输出通道不一致,进行降采样操作
        self.downsample = downsample
        if in_channels != out_channels or stride != 1:
            self.downsample = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(out_channels)
            )

    def forward(self, x):
        identity = x
        if self.downsample is not None:
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        out += identity
        out = self.relu(out)
        return out
In [11]:
class Mlp(nn.Module):
    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU6, drop=0.):
        super().__init__()
        out_features = out_features or in_features
        hidden_features = hidden_features or in_features
        self.fc1 = nn.Conv2d(in_features, hidden_features, 1, 1, 0, bias=True)

        self.act = act_layer()
        self.fc2 = nn.Conv2d(hidden_features, out_features, 1, 1, 0, bias=True)
        self.drop = nn.Dropout(drop, inplace=True)

    def forward(self, x):
        x = self.fc1(x)
        x = self.act(x)
        x = self.drop(x)
        x = self.fc2(x)
        x = self.drop(x)
        return x
In [12]:
class MultiHeadAttentionBlock(nn.Module):
    def __init__(self, embed_dim, num_heads, dropout=0.1):
        super(MultiHeadAttentionBlock, self).__init__()
        self.attention = nn.MultiheadAttention(embed_dim, num_heads, dropout=dropout)
        self.norm = nn.LayerNorm(embed_dim)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        # (B, C, H, W) -> (HW, B, C) for MultiheadAttention compatibility
        B, C, H, W = x.shape
        x = x.view(B, C, H * W).permute(2, 0, 1)  # (B, C, H, W) -> (HW, B, C)

        # Apply multihead attention
        attn_output, _ = self.attention(x, x, x)

        # Apply normalization and dropout
        attn_output = self.norm(attn_output)
        attn_output = self.dropout(attn_output)

        # Reshape back to (B, C, H, W)
        attn_output = attn_output.permute(1, 2, 0).view(B, C, H, W)

        return attn_output
In [13]:
class SpatialAttentionBlock(nn.Module):
    def __init__(self):
        super(SpatialAttentionBlock, self).__init__()
        self.conv = nn.Conv2d(2, 1, kernel_size=7, padding=3, bias=False)

    def forward(self, x): #(B, 64, H, W)
        avg_out = torch.mean(x, dim=1, keepdim=True) #(B, 1, H, W)
        max_out, _ = torch.max(x, dim=1, keepdim=True)#(B, 1, H, W)
        out = torch.cat([avg_out, max_out], dim=1)#(B, 2, H, W)
        out = torch.sigmoid(self.conv(out))#(B, 1, H, W)
        return x * out #(B, C, H, W)
In [14]:
class DecoderAttentionBlock(nn.Module):
    def __init__(self, in_channels):
        super(DecoderAttentionBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, in_channels // 2, kernel_size=1)
        self.conv2 = nn.Conv2d(in_channels // 2, in_channels, kernel_size=1)
        self.spatial_attention = SpatialAttentionBlock()

    def forward(self, x):
        # 通道注意力
        b, c, h, w = x.size()
        avg_pool = F.adaptive_avg_pool2d(x, 1)
        max_pool = F.adaptive_max_pool2d(x, 1)

        avg_out = self.conv1(avg_pool)
        max_out = self.conv1(max_pool)

        out = avg_out + max_out
        out = torch.sigmoid(self.conv2(out))

        # 添加空间注意力
        out = x * out
        out = self.spatial_attention(out)
        return out
In [15]:
class SEBlock(nn.Module):
    def __init__(self, in_channels, reduced_dim):
        super(SEBlock, self).__init__()
        self.se = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),  # 全局平均池化
            nn.Conv2d(in_channels, reduced_dim, kernel_size=1),
            nn.ReLU(),
            nn.Conv2d(reduced_dim, in_channels, kernel_size=1),
            nn.Sigmoid()  # 使用Sigmoid是因为我们要对通道进行权重归一化
        )

    def forward(self, x):
        return x * self.se(x)
In [16]:
def masked_mse_loss(preds, target, mask):
    loss = (preds - target) ** 2
    loss = loss.mean(dim=-1)  # 对每个像素点求平均
    loss = (loss * mask).sum() / mask.sum()  # 只计算被mask的像素点的损失
    return loss
In [17]:
# 定义Masked Autoencoder模型
class MaskedAutoencoder(nn.Module):
    def __init__(self):
        super(MaskedAutoencoder, self).__init__()
        self.encoder = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            SEBlock(128, 128)
        )
        self.mlp = Mlp(in_features=128, hidden_features=256, out_features=128, act_layer=nn.ReLU6, drop=0.1)
        self.decoder = nn.Sequential(
            nn.ConvTranspose2d(128, 32, kernel_size=3, stride=2, padding=1, output_padding=1),
            nn.ReLU(),
            
            DecoderAttentionBlock(32),
            nn.ConvTranspose2d(32, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
            nn.ReLU(),
            
            DecoderAttentionBlock(16),
            nn.ReLU(),
            
            nn.ConvTranspose2d(16, 1, kernel_size=3, stride=2, padding=1, output_padding=1),  # 修改为 output_padding=1
            nn.Sigmoid()
        )

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded

# 实例化模型、损失函数和优化器
model = MaskedAutoencoder()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
In [18]:
# 训练函数
def train_epoch(model, device, data_loader, criterion, optimizer):
    model.train()
    running_loss = 0.0
    for batch_idx, (X, y, mask) in enumerate(data_loader):
        X, y, mask = X.to(device), y.to(device), mask.to(device)
        optimizer.zero_grad()
        reconstructed = model(X)
        # loss = criterion(reconstructed, y)
        loss = masked_mse_loss(reconstructed, y, mask)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    return running_loss / (batch_idx + 1)
In [19]:
# 评估函数
def evaluate(model, device, data_loader, criterion):
    model.eval()
    running_loss = 0.0
    with torch.no_grad():
        for batch_idx, (X, y, mask) in enumerate(data_loader):
            X, y, mask = X.to(device), y.to(device), mask.to(device)
            reconstructed = model(X)
            if batch_idx == 8:
                rand_ind = np.random.randint(0, len(y))
                # visualize_feature(y[rand_ind], X[rand_ind], reconstructed[rand_ind], title='NO_2')
            loss = masked_mse_loss(reconstructed, y, mask)
            running_loss += loss.item()
    return running_loss / (batch_idx + 1)
In [20]:
# 数据准备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
cuda
In [ ]:
model = model.to(device)

num_epochs = 100
train_losses = list()
val_losses = list()
for epoch in range(num_epochs):
    train_loss = train_epoch(model, device, train_loader, criterion, optimizer)
    train_losses.append(train_loss)
    val_loss = evaluate(model, device, val_loader, criterion)
    val_losses.append(val_loss)
    print(f'Epoch {epoch+1}, Train Loss: {train_loss}, Val Loss: {val_loss}')

# 测试模型
test_loss = evaluate(model, device, test_loader, criterion)
print(f'Test Loss: {test_loss}')
/root/miniconda3/envs/python38/lib/python3.8/site-packages/torch/nn/modules/conv.py:456: UserWarning: Applied workaround for CuDNN issue, install nvrtc.so (Triggered internally at /opt/conda/conda-bld/pytorch_1711403590347/work/aten/src/ATen/native/cudnn/Conv_v8.cpp:80.)
  return F.conv2d(input, weight, bias, self.stride,
Epoch 1, Train Loss: 2.1122538542205636, Val Loss: 0.17511643736220117
Epoch 2, Train Loss: 0.09455115371272324, Val Loss: 0.07173499481669113
Epoch 3, Train Loss: 0.05875080322142708, Val Loss: 0.05522163668230398
Epoch 4, Train Loss: 0.04709345177618083, Val Loss: 0.046923332583548416
Epoch 5, Train Loss: 0.04048821633975757, Val Loss: 0.04223129592502295
Epoch 6, Train Loss: 0.03651897717071207, Val Loss: 0.038725908567656335
Epoch 7, Train Loss: 0.03371973283606711, Val Loss: 0.03591352106252713
Epoch 8, Train Loss: 0.030995923611357737, Val Loss: 0.033181621734775714
Epoch 9, Train Loss: 0.02894393834575084, Val Loss: 0.031025866519159347
Epoch 10, Train Loss: 0.026934354539301122, Val Loss: 0.028885239290434923
Epoch 11, Train Loss: 0.025755781114422248, Val Loss: 0.027564026443148728
Epoch 12, Train Loss: 0.024294818880740535, Val Loss: 0.02660573101532993
Epoch 13, Train Loss: 0.023547336254179763, Val Loss: 0.025523469658262694
Epoch 14, Train Loss: 0.02263737249335176, Val Loss: 0.024892248685902625
Epoch 15, Train Loss: 0.02204986723389423, Val Loss: 0.02482297744101553
Epoch 16, Train Loss: 0.021457266258566005, Val Loss: 0.024080637119599242
Epoch 17, Train Loss: 0.020942402789681153, Val Loss: 0.023763289508312496
Epoch 18, Train Loss: 0.02059948215769096, Val Loss: 0.023712928865605325
Epoch 19, Train Loss: 0.020213669665050848, Val Loss: 0.022951017092190572
Epoch 20, Train Loss: 0.02002489379647246, Val Loss: 0.022396566457490424
Epoch 21, Train Loss: 0.019488899257818337, Val Loss: 0.02220052338914195
Epoch 22, Train Loss: 0.019191946226069657, Val Loss: 0.021812534682563882
Epoch 23, Train Loss: 0.018820160999894142, Val Loss: 0.021094122540150115
Epoch 24, Train Loss: 0.01841514516826808, Val Loss: 0.021011906689894732
Epoch 25, Train Loss: 0.01826861325392954, Val Loss: 0.020965722514622247
Epoch 26, Train Loss: 0.01783664010768159, Val Loss: 0.02035376571341237
Epoch 27, Train Loss: 0.01773165784883157, Val Loss: 0.020316684896599
Epoch 28, Train Loss: 0.017462643957362647, Val Loss: 0.020199675196364744
Epoch 29, Train Loss: 0.01726480335237806, Val Loss: 0.019924583983843894
Epoch 30, Train Loss: 0.017130774285412577, Val Loss: 0.019827198264981385
Epoch 31, Train Loss: 0.016821091141302192, Val Loss: 0.01998631670070228
Epoch 32, Train Loss: 0.016754478447887886, Val Loss: 0.019008648901510595
Epoch 33, Train Loss: 0.01657688988452893, Val Loss: 0.01900591877803429
Epoch 34, Train Loss: 0.016496175670613084, Val Loss: 0.019055584264891363
Epoch 35, Train Loss: 0.01644454181470583, Val Loss: 0.018636108959899908
Epoch 36, Train Loss: 0.01607896311823546, Val Loss: 0.018534055174286686
Epoch 37, Train Loss: 0.01588705154224945, Val Loss: 0.018062156513889333
Epoch 38, Train Loss: 0.015864519495962626, Val Loss: 0.018233197171296647
Epoch 39, Train Loss: 0.015855632771394755, Val Loss: 0.018038090332341727
Epoch 40, Train Loss: 0.015651265439982905, Val Loss: 0.01822574678530444
Epoch 41, Train Loss: 0.015510451237996372, Val Loss: 0.017679256400955256
Epoch 42, Train Loss: 0.015349842104436963, Val Loss: 0.018203645916794662
Epoch 43, Train Loss: 0.01543403383451358, Val Loss: 0.017195541675744663
Epoch 44, Train Loss: 0.015325402941233947, Val Loss: 0.017411370608788817
Epoch 45, Train Loss: 0.01518570597876202, Val Loss: 0.017076766354712978
Epoch 46, Train Loss: 0.014841953983182827, Val Loss: 0.016906344637608352
Epoch 47, Train Loss: 0.014843696093356068, Val Loss: 0.016789415712232022
Epoch 48, Train Loss: 0.014590430285104296, Val Loss: 0.01671677505347266
Epoch 49, Train Loss: 0.014620297918158569, Val Loss: 0.01652295997282907
Epoch 50, Train Loss: 0.014581651776654726, Val Loss: 0.01616852485866689
Epoch 51, Train Loss: 0.014414639787026569, Val Loss: 0.016296155653449138
Epoch 52, Train Loss: 0.01424450205157747, Val Loss: 0.016307457906207933
Epoch 53, Train Loss: 0.014137028997238173, Val Loss: 0.01646944234119867
Epoch 54, Train Loss: 0.014159051344939395, Val Loss: 0.016026857336844082
Epoch 55, Train Loss: 0.014192796753425347, Val Loss: 0.01584606984658028
Epoch 56, Train Loss: 0.013916373460076785, Val Loss: 0.015976423856371373
Epoch 57, Train Loss: 0.013736099040394195, Val Loss: 0.015810697172671112
Epoch 58, Train Loss: 0.013836662209276377, Val Loss: 0.015620186396721584
Epoch 59, Train Loss: 0.013784786091413367, Val Loss: 0.015319373792231972
Epoch 60, Train Loss: 0.013611769829497954, Val Loss: 0.015367041216857398
Epoch 61, Train Loss: 0.01358566418931815, Val Loss: 0.015289715783142331
Epoch 62, Train Loss: 0.013467149546093633, Val Loss: 0.015166739780289023
Epoch 63, Train Loss: 0.013366587792019668, Val Loss: 0.014960003544145556
Epoch 64, Train Loss: 0.013362093665971282, Val Loss: 0.015207788253675646
Epoch 65, Train Loss: 0.013282296849352322, Val Loss: 0.015704237049751317
Epoch 66, Train Loss: 0.013314912690553796, Val Loss: 0.015118209617351419
Epoch 67, Train Loss: 0.01314743113610448, Val Loss: 0.014853793154679128
Epoch 68, Train Loss: 0.013220271071125018, Val Loss: 0.015044791985358765
Epoch 69, Train Loss: 0.013089903819700035, Val Loss: 0.014621049485433458
Epoch 70, Train Loss: 0.013003655555591201, Val Loss: 0.015181626902142567
Epoch 71, Train Loss: 0.013071733119153377, Val Loss: 0.014468084979079553
Epoch 72, Train Loss: 0.013008178180555979, Val Loss: 0.014925862592992499
Epoch 73, Train Loss: 0.01300788912521096, Val Loss: 0.015519192122590186
Epoch 74, Train Loss: 0.012897961314001153, Val Loss: 0.014994534872361083
Epoch 75, Train Loss: 0.012850848984632766, Val Loss: 0.014727158249536557
Epoch 76, Train Loss: 0.012889095829380899, Val Loss: 0.014613447293861588
Epoch 77, Train Loss: 0.01279138982447497, Val Loss: 0.014250260944575516
In [25]:
tr_ind = list(range(len(train_losses)))
val_ind = list(range(len(val_losses)))
plt.plot(train_losses, label='train_loss')
plt.plot(val_losses, label='val_loss')
plt.legend(loc='best')
Out[25]:
<matplotlib.legend.Legend at 0x7fb64e455b50>
No description has been provided for this image
In [26]:
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_percentage_error, mean_absolute_error
In [27]:
eva_list = list()
device = 'cpu'
model = model.to(device)
with torch.no_grad():
    for batch_idx, (X, y, mask) in enumerate(test_loader):
        X, y, mask = X.to(device), y.to(device), mask.to(device)
        mask_rev = (torch.squeeze(mask, dim=1)==0) * 1 # mask取反获得修复区域
        reconstructed = model(X)
        # tr_maxs = np.transpose(maxs, (2, 0, 1))
        # tr_mins = np.transpose(mins, (2, 0, 1))
        rev_data = y * max_pixel_value
        rev_recon = reconstructed * max_pixel_value
        # todo: 这里需要只评估修补出来的模块
        data_label = torch.squeeze(rev_data, dim=1) * mask_rev
        data_label = data_label[mask_rev==1]
        recon_no2 = torch.squeeze(rev_recon, dim=1) * mask_rev
        recon_no2 = recon_no2[mask_rev==1]
        mae = mean_absolute_error(data_label, recon_no2)
        rmse = np.sqrt(mean_squared_error(data_label, recon_no2))
        mape = mean_absolute_percentage_error(data_label, recon_no2)
        r2 = r2_score(data_label, recon_no2)
        eva_list.append([mae, rmse, mape, r2])
In [28]:
pd.DataFrame(eva_list, columns=['mae', 'rmse', 'mape', 'r2']).describe()
Out[28]:
mae rmse mape r2
count 75.000000 75.000000 75.000000 75.000000
mean 1.548639 2.513043 0.190712 0.850014
std 0.104697 0.277761 0.018381 0.021919
min 1.372461 2.125686 0.158994 0.766183
25% 1.492424 2.371325 0.177162 0.836254
50% 1.553864 2.482061 0.187778 0.851790
75% 1.600554 2.630040 0.201229 0.865281
max 2.036150 4.280405 0.259433 0.884967
In [34]:
pd.DataFrame(eva_list, columns=['mae', 'rmse', 'mape', 'r2']).describe().to_csv('./eva_files/decoder+local_loss.csv', encoding='utf-8-sig')
In [29]:
def cal_ioa(y_true, y_pred):
    # 计算平均值
    mean_observed = np.mean(y_true)
    mean_predicted = np.mean(y_pred)

    # 计算IoA
    numerator = np.sum((y_true - y_pred) ** 2)
    denominator = np.sum((np.abs(y_true - mean_observed) + np.abs(y_pred - mean_predicted)) ** 2)
    IoA = 1 - (numerator / denominator)

    return IoA
In [30]:
eva_list_frame = list()
device = 'cpu'
model = model.to(device)
with torch.no_grad():
    for batch_idx, (X, y, mask) in enumerate(test_loader):
        X, y, mask = X.to(device), y.to(device), mask.to(device)
        mask_rev = (torch.squeeze(mask, dim=1)==0) * 1 # mask取反获得修复区域
        reconstructed = model(X)
        rev_data = y * max_pixel_value
        rev_recon = reconstructed * max_pixel_value
        # todo: 这里需要只评估修补出来的模块
        for i, sample in enumerate(rev_data):
            used_mask = mask_rev[i]
            data_label = sample[0] * used_mask
            recon_no2 = rev_recon[i][0] * used_mask
            data_label = data_label[used_mask==1]
            recon_no2 = recon_no2[used_mask==1]
            mae = mean_absolute_error(data_label, recon_no2)
            rmse = np.sqrt(mean_squared_error(data_label, recon_no2))
            mape = mean_absolute_percentage_error(data_label, recon_no2)
            r2 = r2_score(data_label, recon_no2)
            ioa = cal_ioa(data_label.detach().numpy(), recon_no2.detach().numpy())
            r = np.corrcoef(data_label, recon_no2)[0, 1]
            eva_list_frame.append([mae, rmse, mape, r2, ioa, r])
In [31]:
pd.DataFrame(eva_list_frame, columns=['mae', 'rmse', 'mape', 'r2', 'ioa', 'r']).describe()
Out[31]:
mae rmse mape r2 ioa r
count 4739.000000 4739.000000 4739.000000 4739.000000 4739.000000 4739.000000
mean 1.553667 2.209092 0.188788 0.523867 0.829028 0.775553
std 0.821044 1.193856 0.121753 0.420704 0.182549 0.164661
min 0.525306 0.680506 0.061413 -4.738533 -0.916011 -0.197854
25% 0.960099 1.333764 0.131694 0.429017 0.802631 0.715950
50% 1.369256 1.958160 0.163652 0.646098 0.889664 0.824197
75% 1.892561 2.704055 0.203364 0.768918 0.931843 0.886272
max 7.905261 11.196068 1.671224 0.972414 0.993103 0.986316
In [ ]: