@icyfenix-dmla/cli 2026.5.2-7 → 2026.5.3-821
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -6
- package/scripts/build.js +44 -11
- package/shared_modules/__init__.py +10 -0
- package/shared_modules/bayesian/__init__.py +6 -0
- package/shared_modules/bayesian/bayesian_network.py +105 -0
- package/shared_modules/bayesian/gaussian_mixture_model.py +141 -0
- package/shared_modules/bayesian/gaussian_mixturemodel.py +141 -0
- package/shared_modules/bayesian/multinomial_naive_bayes.py +74 -0
- package/shared_modules/bayesian/simple_bayesian_network.py +99 -0
- package/shared_modules/bayesian/simple_bayesiannetwork.py +99 -0
- package/shared_modules/cnn/__init__.py +5 -0
- package/shared_modules/cnn/alex_net.py +65 -0
- package/shared_modules/cnn/alexnet.py +65 -0
- package/shared_modules/cnn/t_e_r_m1.py +65 -0
- package/shared_modules/cnn/tiny_image_net_dataset.py +67 -0
- package/shared_modules/cnn/tiny_imagenet_dataset.py +67 -0
- package/shared_modules/cnn/tiny_imagenetdataset.py +67 -0
- package/shared_modules/cnn/tinyimagenetdataset.py +67 -0
- package/shared_modules/linear/__init__.py +6 -0
- package/shared_modules/linear/lasso_regression.py +93 -0
- package/shared_modules/linear/logistic_regression.py +78 -0
- package/shared_modules/linear/naive_bayes.py +141 -0
- package/shared_modules/linear/ridge_regression.py +58 -0
- package/shared_modules/neural/__init__.py +4 -0
- package/shared_modules/neural/perceptron.py +80 -0
- package/shared_modules/svm/__init__.py +5 -0
- package/shared_modules/svm/kernel_s_v_m.py +98 -0
- package/shared_modules/svm/kernel_svm.py +98 -0
- package/shared_modules/svm/simple_s_v_m.py +111 -0
- package/shared_modules/svm/simple_svm.py +111 -0
- package/shared_modules/tree/__init__.py +6 -0
- package/shared_modules/tree/ada_boost.py +77 -0
- package/shared_modules/tree/decision_tree_classifier.py +235 -0
- package/shared_modules/tree/decision_treeclassifier.py +235 -0
- package/shared_modules/tree/random_forest_classifier.py +88 -0
- package/shared_modules/tree/random_forestclassifier.py +88 -0
- package/shared_modules/unsupervised/__init__.py +5 -0
- package/shared_modules/unsupervised/k_means.py +127 -0
- package/shared_modules/unsupervised/kmeans.py +127 -0
- package/shared_modules/unsupervised/p_c_a.py +111 -0
- package/shared_modules/unsupervised/pca.py +111 -0
- package/src/commands/data.js +823 -0
- package/src/commands/server.js +209 -4
- package/src/index.js +23 -2
- package/src/server/routes/sandbox.js +70 -3
- package/src/server/sandbox.js +87 -11
- package/version.json +4 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# SimpleBayesianNetwork 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
class SimpleBayesianNetwork:
|
|
5
|
+
"""
|
|
6
|
+
简单贝叶斯网络实现
|
|
7
|
+
支持离散变量和精确推断(枚举法)
|
|
8
|
+
"""
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.nodes = {} # 节点信息:{name: {'parents': [], 'values': []}}
|
|
11
|
+
self.cpts = {} # 条件概率表:{name: {parent_values: {value: prob}}}
|
|
12
|
+
self.topo_order = [] # 拓扑排序
|
|
13
|
+
|
|
14
|
+
def add_node(self, name, values, parents=None):
|
|
15
|
+
"""添加节点"""
|
|
16
|
+
if parents is None:
|
|
17
|
+
parents = []
|
|
18
|
+
self.nodes[name] = {'parents': parents, 'values': values}
|
|
19
|
+
self._update_topo_order()
|
|
20
|
+
|
|
21
|
+
def set_cpt(self, name, cpt):
|
|
22
|
+
"""
|
|
23
|
+
设置条件概率表
|
|
24
|
+
|
|
25
|
+
cpt格式:{parent_value_tuple: {value: prob}}
|
|
26
|
+
对于无父节点的变量:{(): {value: prob}}
|
|
27
|
+
"""
|
|
28
|
+
self.cpts[name] = cpt
|
|
29
|
+
|
|
30
|
+
def _update_topo_order(self):
|
|
31
|
+
"""计算拓扑排序"""
|
|
32
|
+
visited = set()
|
|
33
|
+
order = []
|
|
34
|
+
|
|
35
|
+
def visit(node):
|
|
36
|
+
if node in visited:
|
|
37
|
+
return
|
|
38
|
+
visited.add(node)
|
|
39
|
+
for parent in self.nodes[node]['parents']:
|
|
40
|
+
visit(parent)
|
|
41
|
+
order.append(node)
|
|
42
|
+
|
|
43
|
+
for node in self.nodes:
|
|
44
|
+
visit(node)
|
|
45
|
+
|
|
46
|
+
self.topo_order = order
|
|
47
|
+
|
|
48
|
+
def get_prob(self, name, value, parent_values):
|
|
49
|
+
"""获取条件概率 P(name=value | parent_values)"""
|
|
50
|
+
parent_key = tuple(parent_values) if parent_values else ()
|
|
51
|
+
return self.cpts[name].get(parent_key, {}).get(value, 0)
|
|
52
|
+
|
|
53
|
+
def joint_prob(self, assignment):
|
|
54
|
+
"""计算联合概率 P(X1, X2, ...)"""
|
|
55
|
+
prob = 1.0
|
|
56
|
+
for node in self.topo_order:
|
|
57
|
+
parents = self.nodes[node]['parents']
|
|
58
|
+
parent_values = [assignment[p] for p in parents]
|
|
59
|
+
value = assignment[node]
|
|
60
|
+
prob *= self.get_prob(node, value, parent_values)
|
|
61
|
+
return prob
|
|
62
|
+
|
|
63
|
+
def enumerate_inference(self, query, evidence):
|
|
64
|
+
"""
|
|
65
|
+
枚举推断:计算 P(query | evidence)
|
|
66
|
+
|
|
67
|
+
query: {node: '?'} 返回分布
|
|
68
|
+
evidence: {node: value}
|
|
69
|
+
"""
|
|
70
|
+
query_nodes = list(query.keys())
|
|
71
|
+
hidden = [n for n in self.nodes if n not in query_nodes and n not in evidence]
|
|
72
|
+
|
|
73
|
+
def enumerate_assignments(variables, current):
|
|
74
|
+
if not variables:
|
|
75
|
+
yield current.copy()
|
|
76
|
+
return
|
|
77
|
+
var = variables[0]
|
|
78
|
+
for value in self.nodes[var]['values']:
|
|
79
|
+
current[var] = value
|
|
80
|
+
yield from enumerate_assignments(variables[1:], current)
|
|
81
|
+
del current[var]
|
|
82
|
+
|
|
83
|
+
query_values = {}
|
|
84
|
+
total = 0.0
|
|
85
|
+
|
|
86
|
+
query_node = query_nodes[0]
|
|
87
|
+
for qv in self.nodes[query_node]['values']:
|
|
88
|
+
prob_sum = 0.0
|
|
89
|
+
for assignment in enumerate_assignments(hidden, {}):
|
|
90
|
+
assignment.update(evidence)
|
|
91
|
+
assignment[query_node] = qv
|
|
92
|
+
prob_sum += self.joint_prob(assignment)
|
|
93
|
+
query_values[qv] = prob_sum
|
|
94
|
+
total += prob_sum
|
|
95
|
+
|
|
96
|
+
# 归一化
|
|
97
|
+
for k in query_values:
|
|
98
|
+
query_values[k] /= total
|
|
99
|
+
return query_values
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# SimpleBayesianNetwork 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
class SimpleBayesianNetwork:
|
|
5
|
+
"""
|
|
6
|
+
简单贝叶斯网络实现
|
|
7
|
+
支持离散变量和精确推断(枚举法)
|
|
8
|
+
"""
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.nodes = {} # 节点信息:{name: {'parents': [], 'values': []}}
|
|
11
|
+
self.cpts = {} # 条件概率表:{name: {parent_values: {value: prob}}}
|
|
12
|
+
self.topo_order = [] # 拓扑排序
|
|
13
|
+
|
|
14
|
+
def add_node(self, name, values, parents=None):
|
|
15
|
+
"""添加节点"""
|
|
16
|
+
if parents is None:
|
|
17
|
+
parents = []
|
|
18
|
+
self.nodes[name] = {'parents': parents, 'values': values}
|
|
19
|
+
self._update_topo_order()
|
|
20
|
+
|
|
21
|
+
def set_cpt(self, name, cpt):
|
|
22
|
+
"""
|
|
23
|
+
设置条件概率表
|
|
24
|
+
|
|
25
|
+
cpt格式:{parent_value_tuple: {value: prob}}
|
|
26
|
+
对于无父节点的变量:{(): {value: prob}}
|
|
27
|
+
"""
|
|
28
|
+
self.cpts[name] = cpt
|
|
29
|
+
|
|
30
|
+
def _update_topo_order(self):
|
|
31
|
+
"""计算拓扑排序"""
|
|
32
|
+
visited = set()
|
|
33
|
+
order = []
|
|
34
|
+
|
|
35
|
+
def visit(node):
|
|
36
|
+
if node in visited:
|
|
37
|
+
return
|
|
38
|
+
visited.add(node)
|
|
39
|
+
for parent in self.nodes[node]['parents']:
|
|
40
|
+
visit(parent)
|
|
41
|
+
order.append(node)
|
|
42
|
+
|
|
43
|
+
for node in self.nodes:
|
|
44
|
+
visit(node)
|
|
45
|
+
|
|
46
|
+
self.topo_order = order
|
|
47
|
+
|
|
48
|
+
def get_prob(self, name, value, parent_values):
|
|
49
|
+
"""获取条件概率 P(name=value | parent_values)"""
|
|
50
|
+
parent_key = tuple(parent_values) if parent_values else ()
|
|
51
|
+
return self.cpts[name].get(parent_key, {}).get(value, 0)
|
|
52
|
+
|
|
53
|
+
def joint_prob(self, assignment):
|
|
54
|
+
"""计算联合概率 P(X1, X2, ...)"""
|
|
55
|
+
prob = 1.0
|
|
56
|
+
for node in self.topo_order:
|
|
57
|
+
parents = self.nodes[node]['parents']
|
|
58
|
+
parent_values = [assignment[p] for p in parents]
|
|
59
|
+
value = assignment[node]
|
|
60
|
+
prob *= self.get_prob(node, value, parent_values)
|
|
61
|
+
return prob
|
|
62
|
+
|
|
63
|
+
def enumerate_inference(self, query, evidence):
|
|
64
|
+
"""
|
|
65
|
+
枚举推断:计算 P(query | evidence)
|
|
66
|
+
|
|
67
|
+
query: {node: '?'} 返回分布
|
|
68
|
+
evidence: {node: value}
|
|
69
|
+
"""
|
|
70
|
+
query_nodes = list(query.keys())
|
|
71
|
+
hidden = [n for n in self.nodes if n not in query_nodes and n not in evidence]
|
|
72
|
+
|
|
73
|
+
def enumerate_assignments(variables, current):
|
|
74
|
+
if not variables:
|
|
75
|
+
yield current.copy()
|
|
76
|
+
return
|
|
77
|
+
var = variables[0]
|
|
78
|
+
for value in self.nodes[var]['values']:
|
|
79
|
+
current[var] = value
|
|
80
|
+
yield from enumerate_assignments(variables[1:], current)
|
|
81
|
+
del current[var]
|
|
82
|
+
|
|
83
|
+
query_values = {}
|
|
84
|
+
total = 0.0
|
|
85
|
+
|
|
86
|
+
query_node = query_nodes[0]
|
|
87
|
+
for qv in self.nodes[query_node]['values']:
|
|
88
|
+
prob_sum = 0.0
|
|
89
|
+
for assignment in enumerate_assignments(hidden, {}):
|
|
90
|
+
assignment.update(evidence)
|
|
91
|
+
assignment[query_node] = qv
|
|
92
|
+
prob_sum += self.joint_prob(assignment)
|
|
93
|
+
query_values[qv] = prob_sum
|
|
94
|
+
total += prob_sum
|
|
95
|
+
|
|
96
|
+
# 归一化
|
|
97
|
+
for k in query_values:
|
|
98
|
+
query_values[k] /= total
|
|
99
|
+
return query_values
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# AlexNet 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn as nn
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
class AlexNet(nn.Module):
|
|
9
|
+
"""
|
|
10
|
+
AlexNet 网络结构
|
|
11
|
+
适配 Tiny ImageNet 200 类分类任务
|
|
12
|
+
|
|
13
|
+
原始 AlexNet 为 1000 类,这里修改最后一层为 200 类
|
|
14
|
+
使用 AdaptiveAvgPool2d 确保输出尺寸固定为 6x6
|
|
15
|
+
"""
|
|
16
|
+
def __init__(self, num_classes=200):
|
|
17
|
+
super(AlexNet, self).__init__()
|
|
18
|
+
|
|
19
|
+
# 特征提取层 (5 个卷积层)
|
|
20
|
+
self.features = nn.Sequential(
|
|
21
|
+
# Conv1: 11x11 卷积,步长 4,输出 96 通道
|
|
22
|
+
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
|
|
23
|
+
nn.ReLU(inplace=True),
|
|
24
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
25
|
+
|
|
26
|
+
# Conv2: 5x5 卷积,输出 256 通道
|
|
27
|
+
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
|
|
28
|
+
nn.ReLU(inplace=True),
|
|
29
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
30
|
+
|
|
31
|
+
# Conv3: 3x3 卷积,输出 384 通道
|
|
32
|
+
nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
|
|
33
|
+
nn.ReLU(inplace=True),
|
|
34
|
+
|
|
35
|
+
# Conv4: 3x3 卷积,输出 384 通道
|
|
36
|
+
nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
|
|
37
|
+
nn.ReLU(inplace=True),
|
|
38
|
+
|
|
39
|
+
# Conv5: 3x3 卷积,输出 256 通道
|
|
40
|
+
nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
|
|
41
|
+
nn.ReLU(inplace=True),
|
|
42
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
43
|
+
|
|
44
|
+
# 自适应池化,确保输出固定为 6x6
|
|
45
|
+
nn.AdaptiveAvgPool2d((6, 6))
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# 分类层 (3 个全连接层)
|
|
49
|
+
self.classifier = nn.Sequential(
|
|
50
|
+
nn.Dropout(p=0.5),
|
|
51
|
+
nn.Linear(256 * 6 * 6, 4096),
|
|
52
|
+
nn.ReLU(inplace=True),
|
|
53
|
+
|
|
54
|
+
nn.Dropout(p=0.5),
|
|
55
|
+
nn.Linear(4096, 4096),
|
|
56
|
+
nn.ReLU(inplace=True),
|
|
57
|
+
|
|
58
|
+
nn.Linear(4096, num_classes)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def forward(self, x):
|
|
62
|
+
x = self.features(x)
|
|
63
|
+
x = torch.flatten(x, 1)
|
|
64
|
+
x = self.classifier(x)
|
|
65
|
+
return x
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# AlexNet 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn as nn
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
class AlexNet(nn.Module):
|
|
9
|
+
"""
|
|
10
|
+
AlexNet 网络结构
|
|
11
|
+
适配 Tiny ImageNet 200 类分类任务
|
|
12
|
+
|
|
13
|
+
原始 AlexNet 为 1000 类,这里修改最后一层为 200 类
|
|
14
|
+
使用 AdaptiveAvgPool2d 确保输出尺寸固定为 6x6
|
|
15
|
+
"""
|
|
16
|
+
def __init__(self, num_classes=200):
|
|
17
|
+
super(AlexNet, self).__init__()
|
|
18
|
+
|
|
19
|
+
# 特征提取层 (5 个卷积层)
|
|
20
|
+
self.features = nn.Sequential(
|
|
21
|
+
# Conv1: 11x11 卷积,步长 4,输出 96 通道
|
|
22
|
+
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
|
|
23
|
+
nn.ReLU(inplace=True),
|
|
24
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
25
|
+
|
|
26
|
+
# Conv2: 5x5 卷积,输出 256 通道
|
|
27
|
+
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
|
|
28
|
+
nn.ReLU(inplace=True),
|
|
29
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
30
|
+
|
|
31
|
+
# Conv3: 3x3 卷积,输出 384 通道
|
|
32
|
+
nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
|
|
33
|
+
nn.ReLU(inplace=True),
|
|
34
|
+
|
|
35
|
+
# Conv4: 3x3 卷积,输出 384 通道
|
|
36
|
+
nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
|
|
37
|
+
nn.ReLU(inplace=True),
|
|
38
|
+
|
|
39
|
+
# Conv5: 3x3 卷积,输出 256 通道
|
|
40
|
+
nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
|
|
41
|
+
nn.ReLU(inplace=True),
|
|
42
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
43
|
+
|
|
44
|
+
# 自适应池化,确保输出固定为 6x6
|
|
45
|
+
nn.AdaptiveAvgPool2d((6, 6))
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# 分类层 (3 个全连接层)
|
|
49
|
+
self.classifier = nn.Sequential(
|
|
50
|
+
nn.Dropout(p=0.5),
|
|
51
|
+
nn.Linear(256 * 6 * 6, 4096),
|
|
52
|
+
nn.ReLU(inplace=True),
|
|
53
|
+
|
|
54
|
+
nn.Dropout(p=0.5),
|
|
55
|
+
nn.Linear(4096, 4096),
|
|
56
|
+
nn.ReLU(inplace=True),
|
|
57
|
+
|
|
58
|
+
nn.Linear(4096, num_classes)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def forward(self, x):
|
|
62
|
+
x = self.features(x)
|
|
63
|
+
x = torch.flatten(x, 1)
|
|
64
|
+
x = self.classifier(x)
|
|
65
|
+
return x
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# AlexNet 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn as nn
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
class AlexNet(nn.Module):
|
|
9
|
+
"""
|
|
10
|
+
AlexNet 网络结构
|
|
11
|
+
适配 Tiny ImageNet 200 类分类任务
|
|
12
|
+
|
|
13
|
+
原始 AlexNet 为 1000 类,这里修改最后一层为 200 类
|
|
14
|
+
使用 AdaptiveAvgPool2d 确保输出尺寸固定为 6x6
|
|
15
|
+
"""
|
|
16
|
+
def __init__(self, num_classes=200):
|
|
17
|
+
super(AlexNet, self).__init__()
|
|
18
|
+
|
|
19
|
+
# 特征提取层 (5 个卷积层)
|
|
20
|
+
self.features = nn.Sequential(
|
|
21
|
+
# Conv1: 11x11 卷积,步长 4,输出 96 通道
|
|
22
|
+
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
|
|
23
|
+
nn.ReLU(inplace=True),
|
|
24
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
25
|
+
|
|
26
|
+
# Conv2: 5x5 卷积,输出 256 通道
|
|
27
|
+
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
|
|
28
|
+
nn.ReLU(inplace=True),
|
|
29
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
30
|
+
|
|
31
|
+
# Conv3: 3x3 卷积,输出 384 通道
|
|
32
|
+
nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
|
|
33
|
+
nn.ReLU(inplace=True),
|
|
34
|
+
|
|
35
|
+
# Conv4: 3x3 卷积,输出 384 通道
|
|
36
|
+
nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
|
|
37
|
+
nn.ReLU(inplace=True),
|
|
38
|
+
|
|
39
|
+
# Conv5: 3x3 卷积,输出 256 通道
|
|
40
|
+
nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
|
|
41
|
+
nn.ReLU(inplace=True),
|
|
42
|
+
nn.MaxPool2d(kernel_size=3, stride=2),
|
|
43
|
+
|
|
44
|
+
# 自适应池化,确保输出固定为 6x6
|
|
45
|
+
nn.AdaptiveAvgPool2d((6, 6))
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# 分类层 (3 个全连接层)
|
|
49
|
+
self.classifier = nn.Sequential(
|
|
50
|
+
nn.Dropout(p=0.5),
|
|
51
|
+
nn.Linear(256 * 6 * 6, 4096),
|
|
52
|
+
nn.ReLU(inplace=True),
|
|
53
|
+
|
|
54
|
+
nn.Dropout(p=0.5),
|
|
55
|
+
nn.Linear(4096, 4096),
|
|
56
|
+
nn.ReLU(inplace=True),
|
|
57
|
+
|
|
58
|
+
nn.Linear(4096, num_classes)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def forward(self, x):
|
|
62
|
+
x = self.features(x)
|
|
63
|
+
x = torch.flatten(x, 1)
|
|
64
|
+
x = self.classifier(x)
|
|
65
|
+
return x
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# TinyImageNetDataset 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from PIL import Image
|
|
6
|
+
from torch.utils.data import Dataset, DataLoader
|
|
7
|
+
|
|
8
|
+
class TinyImageNetDataset(Dataset):
|
|
9
|
+
"""
|
|
10
|
+
Tiny ImageNet 200 数据集加载器
|
|
11
|
+
|
|
12
|
+
训练集按类别子目录读取,验证集从标注文件解析标签。
|
|
13
|
+
支持自定义预处理变换,适配 AlexNet 训练需求。
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, root_dir, transform=None, is_train=True):
|
|
16
|
+
self.root_dir = root_dir
|
|
17
|
+
self.transform = transform
|
|
18
|
+
self.is_train = is_train
|
|
19
|
+
|
|
20
|
+
self.samples = []
|
|
21
|
+
self.classes = []
|
|
22
|
+
|
|
23
|
+
if is_train:
|
|
24
|
+
train_dir = os.path.join(root_dir, 'train')
|
|
25
|
+
self.classes = sorted(os.listdir(train_dir))
|
|
26
|
+
self.class_to_idx = {cls: idx for idx, cls in enumerate(self.classes)}
|
|
27
|
+
|
|
28
|
+
for cls in self.classes:
|
|
29
|
+
cls_dir = os.path.join(train_dir, cls)
|
|
30
|
+
images_dir = os.path.join(cls_dir, 'images')
|
|
31
|
+
if os.path.exists(images_dir):
|
|
32
|
+
for img_name in os.listdir(images_dir):
|
|
33
|
+
if img_name.endswith('.JPEG'):
|
|
34
|
+
self.samples.append((
|
|
35
|
+
os.path.join(images_dir, img_name),
|
|
36
|
+
self.class_to_idx[cls]
|
|
37
|
+
))
|
|
38
|
+
else:
|
|
39
|
+
val_dir = os.path.join(root_dir, 'val')
|
|
40
|
+
val_images_dir = os.path.join(val_dir, 'images')
|
|
41
|
+
val_annotations = os.path.join(val_dir, 'val_annotations.txt')
|
|
42
|
+
|
|
43
|
+
if os.path.exists(val_annotations):
|
|
44
|
+
with open(val_annotations, 'r') as f:
|
|
45
|
+
for line in f:
|
|
46
|
+
parts = line.strip().split('\t')
|
|
47
|
+
if len(parts) >= 2:
|
|
48
|
+
img_name = parts[0]
|
|
49
|
+
cls = parts[1]
|
|
50
|
+
if cls not in self.classes:
|
|
51
|
+
self.classes.append(cls)
|
|
52
|
+
self.samples.append((
|
|
53
|
+
os.path.join(val_images_dir, img_name),
|
|
54
|
+
self.classes.index(cls)
|
|
55
|
+
))
|
|
56
|
+
|
|
57
|
+
def __len__(self):
|
|
58
|
+
return len(self.samples)
|
|
59
|
+
|
|
60
|
+
def __getitem__(self, idx):
|
|
61
|
+
img_path, label = self.samples[idx]
|
|
62
|
+
image = Image.open(img_path).convert('RGB')
|
|
63
|
+
|
|
64
|
+
if self.transform:
|
|
65
|
+
image = self.transform(image)
|
|
66
|
+
|
|
67
|
+
return image, label
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# TinyImageNetDataset 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from PIL import Image
|
|
6
|
+
from torch.utils.data import Dataset, DataLoader
|
|
7
|
+
|
|
8
|
+
class TinyImageNetDataset(Dataset):
|
|
9
|
+
"""
|
|
10
|
+
Tiny ImageNet 200 数据集加载器
|
|
11
|
+
|
|
12
|
+
训练集按类别子目录读取,验证集从标注文件解析标签。
|
|
13
|
+
支持自定义预处理变换,适配 AlexNet 训练需求。
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, root_dir, transform=None, is_train=True):
|
|
16
|
+
self.root_dir = root_dir
|
|
17
|
+
self.transform = transform
|
|
18
|
+
self.is_train = is_train
|
|
19
|
+
|
|
20
|
+
self.samples = []
|
|
21
|
+
self.classes = []
|
|
22
|
+
|
|
23
|
+
if is_train:
|
|
24
|
+
train_dir = os.path.join(root_dir, 'train')
|
|
25
|
+
self.classes = sorted(os.listdir(train_dir))
|
|
26
|
+
self.class_to_idx = {cls: idx for idx, cls in enumerate(self.classes)}
|
|
27
|
+
|
|
28
|
+
for cls in self.classes:
|
|
29
|
+
cls_dir = os.path.join(train_dir, cls)
|
|
30
|
+
images_dir = os.path.join(cls_dir, 'images')
|
|
31
|
+
if os.path.exists(images_dir):
|
|
32
|
+
for img_name in os.listdir(images_dir):
|
|
33
|
+
if img_name.endswith('.JPEG'):
|
|
34
|
+
self.samples.append((
|
|
35
|
+
os.path.join(images_dir, img_name),
|
|
36
|
+
self.class_to_idx[cls]
|
|
37
|
+
))
|
|
38
|
+
else:
|
|
39
|
+
val_dir = os.path.join(root_dir, 'val')
|
|
40
|
+
val_images_dir = os.path.join(val_dir, 'images')
|
|
41
|
+
val_annotations = os.path.join(val_dir, 'val_annotations.txt')
|
|
42
|
+
|
|
43
|
+
if os.path.exists(val_annotations):
|
|
44
|
+
with open(val_annotations, 'r') as f:
|
|
45
|
+
for line in f:
|
|
46
|
+
parts = line.strip().split('\t')
|
|
47
|
+
if len(parts) >= 2:
|
|
48
|
+
img_name = parts[0]
|
|
49
|
+
cls = parts[1]
|
|
50
|
+
if cls not in self.classes:
|
|
51
|
+
self.classes.append(cls)
|
|
52
|
+
self.samples.append((
|
|
53
|
+
os.path.join(val_images_dir, img_name),
|
|
54
|
+
self.classes.index(cls)
|
|
55
|
+
))
|
|
56
|
+
|
|
57
|
+
def __len__(self):
|
|
58
|
+
return len(self.samples)
|
|
59
|
+
|
|
60
|
+
def __getitem__(self, idx):
|
|
61
|
+
img_path, label = self.samples[idx]
|
|
62
|
+
image = Image.open(img_path).convert('RGB')
|
|
63
|
+
|
|
64
|
+
if self.transform:
|
|
65
|
+
image = self.transform(image)
|
|
66
|
+
|
|
67
|
+
return image, label
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# TinyImageNetDataset 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from PIL import Image
|
|
6
|
+
from torch.utils.data import Dataset, DataLoader
|
|
7
|
+
|
|
8
|
+
class TinyImageNetDataset(Dataset):
|
|
9
|
+
"""
|
|
10
|
+
Tiny ImageNet 200 数据集加载器
|
|
11
|
+
|
|
12
|
+
训练集按类别子目录读取,验证集从标注文件解析标签。
|
|
13
|
+
支持自定义预处理变换,适配 AlexNet 训练需求。
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, root_dir, transform=None, is_train=True):
|
|
16
|
+
self.root_dir = root_dir
|
|
17
|
+
self.transform = transform
|
|
18
|
+
self.is_train = is_train
|
|
19
|
+
|
|
20
|
+
self.samples = []
|
|
21
|
+
self.classes = []
|
|
22
|
+
|
|
23
|
+
if is_train:
|
|
24
|
+
train_dir = os.path.join(root_dir, 'train')
|
|
25
|
+
self.classes = sorted(os.listdir(train_dir))
|
|
26
|
+
self.class_to_idx = {cls: idx for idx, cls in enumerate(self.classes)}
|
|
27
|
+
|
|
28
|
+
for cls in self.classes:
|
|
29
|
+
cls_dir = os.path.join(train_dir, cls)
|
|
30
|
+
images_dir = os.path.join(cls_dir, 'images')
|
|
31
|
+
if os.path.exists(images_dir):
|
|
32
|
+
for img_name in os.listdir(images_dir):
|
|
33
|
+
if img_name.endswith('.JPEG'):
|
|
34
|
+
self.samples.append((
|
|
35
|
+
os.path.join(images_dir, img_name),
|
|
36
|
+
self.class_to_idx[cls]
|
|
37
|
+
))
|
|
38
|
+
else:
|
|
39
|
+
val_dir = os.path.join(root_dir, 'val')
|
|
40
|
+
val_images_dir = os.path.join(val_dir, 'images')
|
|
41
|
+
val_annotations = os.path.join(val_dir, 'val_annotations.txt')
|
|
42
|
+
|
|
43
|
+
if os.path.exists(val_annotations):
|
|
44
|
+
with open(val_annotations, 'r') as f:
|
|
45
|
+
for line in f:
|
|
46
|
+
parts = line.strip().split('\t')
|
|
47
|
+
if len(parts) >= 2:
|
|
48
|
+
img_name = parts[0]
|
|
49
|
+
cls = parts[1]
|
|
50
|
+
if cls not in self.classes:
|
|
51
|
+
self.classes.append(cls)
|
|
52
|
+
self.samples.append((
|
|
53
|
+
os.path.join(val_images_dir, img_name),
|
|
54
|
+
self.classes.index(cls)
|
|
55
|
+
))
|
|
56
|
+
|
|
57
|
+
def __len__(self):
|
|
58
|
+
return len(self.samples)
|
|
59
|
+
|
|
60
|
+
def __getitem__(self, idx):
|
|
61
|
+
img_path, label = self.samples[idx]
|
|
62
|
+
image = Image.open(img_path).convert('RGB')
|
|
63
|
+
|
|
64
|
+
if self.transform:
|
|
65
|
+
image = self.transform(image)
|
|
66
|
+
|
|
67
|
+
return image, label
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# TinyImageNetDataset 类定义
|
|
2
|
+
# 从文档自动提取生成
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from PIL import Image
|
|
6
|
+
from torch.utils.data import Dataset, DataLoader
|
|
7
|
+
|
|
8
|
+
class TinyImageNetDataset(Dataset):
|
|
9
|
+
"""
|
|
10
|
+
Tiny ImageNet 200 数据集加载器
|
|
11
|
+
|
|
12
|
+
训练集按类别子目录读取,验证集从标注文件解析标签。
|
|
13
|
+
支持自定义预处理变换,适配 AlexNet 训练需求。
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, root_dir, transform=None, is_train=True):
|
|
16
|
+
self.root_dir = root_dir
|
|
17
|
+
self.transform = transform
|
|
18
|
+
self.is_train = is_train
|
|
19
|
+
|
|
20
|
+
self.samples = []
|
|
21
|
+
self.classes = []
|
|
22
|
+
|
|
23
|
+
if is_train:
|
|
24
|
+
train_dir = os.path.join(root_dir, 'train')
|
|
25
|
+
self.classes = sorted(os.listdir(train_dir))
|
|
26
|
+
self.class_to_idx = {cls: idx for idx, cls in enumerate(self.classes)}
|
|
27
|
+
|
|
28
|
+
for cls in self.classes:
|
|
29
|
+
cls_dir = os.path.join(train_dir, cls)
|
|
30
|
+
images_dir = os.path.join(cls_dir, 'images')
|
|
31
|
+
if os.path.exists(images_dir):
|
|
32
|
+
for img_name in os.listdir(images_dir):
|
|
33
|
+
if img_name.endswith('.JPEG'):
|
|
34
|
+
self.samples.append((
|
|
35
|
+
os.path.join(images_dir, img_name),
|
|
36
|
+
self.class_to_idx[cls]
|
|
37
|
+
))
|
|
38
|
+
else:
|
|
39
|
+
val_dir = os.path.join(root_dir, 'val')
|
|
40
|
+
val_images_dir = os.path.join(val_dir, 'images')
|
|
41
|
+
val_annotations = os.path.join(val_dir, 'val_annotations.txt')
|
|
42
|
+
|
|
43
|
+
if os.path.exists(val_annotations):
|
|
44
|
+
with open(val_annotations, 'r') as f:
|
|
45
|
+
for line in f:
|
|
46
|
+
parts = line.strip().split('\t')
|
|
47
|
+
if len(parts) >= 2:
|
|
48
|
+
img_name = parts[0]
|
|
49
|
+
cls = parts[1]
|
|
50
|
+
if cls not in self.classes:
|
|
51
|
+
self.classes.append(cls)
|
|
52
|
+
self.samples.append((
|
|
53
|
+
os.path.join(val_images_dir, img_name),
|
|
54
|
+
self.classes.index(cls)
|
|
55
|
+
))
|
|
56
|
+
|
|
57
|
+
def __len__(self):
|
|
58
|
+
return len(self.samples)
|
|
59
|
+
|
|
60
|
+
def __getitem__(self, idx):
|
|
61
|
+
img_path, label = self.samples[idx]
|
|
62
|
+
image = Image.open(img_path).convert('RGB')
|
|
63
|
+
|
|
64
|
+
if self.transform:
|
|
65
|
+
image = self.transform(image)
|
|
66
|
+
|
|
67
|
+
return image, label
|