From 71ad0e4696cc14d25c2b9b9f5025f5f6fe136410 Mon Sep 17 00:00:00 2001 From: lemonviv Date: Thu, 26 Dec 2024 10:38:09 +0800 Subject: [PATCH] Update the train file for TED CT Detection application --- .../application/TED_CT_Detection/model.py | 119 ------------------ .../application/TED_CT_Detection/train.py | 35 ++++-- 2 files changed, 23 insertions(+), 131 deletions(-) delete mode 100644 examples/healthcare/application/TED_CT_Detection/model.py diff --git a/examples/healthcare/application/TED_CT_Detection/model.py b/examples/healthcare/application/TED_CT_Detection/model.py deleted file mode 100644 index 1c136d716..000000000 --- a/examples/healthcare/application/TED_CT_Detection/model.py +++ /dev/null @@ -1,119 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -from singa import layer -from singa import model -import singa.tensor as tensor -from singa import autograd -from singa.tensor import Tensor - - -class CPLayer(layer.Layer): - def __init__(self, prototype_count=2, temp=10.0): - super(CPLayer, self).__init__() - self.prototype_count = prototype_count - self.temp = temp - - def initialize(self, x): - self.feature_dim = x.shape[1] - self.prototype = tensor.random( - (self.feature_dim, self.prototype_count), device=x.device - ) - - def forward(self, feat): - self.device_check(feat, self.prototype) - self.dtype_check(feat, self.prototype) - - feat_sq = autograd.mul(feat, feat) - feat_sq_sum = autograd.reduce_sum(feat_sq, axes=[1], keepdims=1) - feat_sq_sum_tile = autograd.tile(feat_sq_sum, repeats=[1, self.feature_dim]) - - prototype_sq = autograd.mul(self.prototype, self.prototype) - prototype_sq_sum = autograd.reduce_sum(prototype_sq, axes=[0], keepdims=1) - prototype_sq_sum_tile = autograd.tile(prototype_sq_sum, repeats=feat.shape[0]) - - cross_term = autograd.matmul(feat, self.prototype) - cross_term_scale = Tensor( - shape=cross_term.shape, device=cross_term.device, requires_grad=False - ).set_value(-2) - cross_term_scaled = autograd.mul(cross_term, cross_term_scale) - - dist = autograd.add(feat_sq_sum_tile, prototype_sq_sum_tile) - dist = autograd.add(dist, cross_term_scaled) - - logits_coeff = ( - tensor.ones((feat.shape[0], self.prototype.shape[1]), device=feat.device) - * -1.0 - / self.temp - ) - logits_coeff.requires_grad = False - logits = autograd.mul(logits_coeff, dist) - - return logits - - def get_params(self): - return {self.prototype.name: self.prototype} - - def set_params(self, parameters): - self.prototype.copy_from(parameters[self.prototype.name]) - - -class CPL(model.Model): - - def __init__( - self, - backbone: model.Model, - prototype_count=2, - lamb=0.5, - temp=10, - label=None, - prototype_weight=None, - ): - super(CPL, self).__init__() - # config - self.lamb = lamb - self.prototype_weight = prototype_weight - self.prototype_label = label - - # layer - self.backbone = backbone - self.cplayer = CPLayer(prototype_count=prototype_count, temp=temp) - # optimizer - self.softmax_cross_entropy = layer.SoftMaxCrossEntropy() - - def forward(self, x): - feat = self.backbone.forward(x) - logits = self.cplayer(feat) - return logits - - def train_one_batch(self, x, y): - out = self.forward(x) - loss = self.softmax_cross_entropy(out, y) - self.optimizer(loss) - return out, loss - - def set_optimizer(self, optimizer): - self.optimizer = optimizer - - -def create_model(backbone, prototype_count=2, lamb=0.5, temp=10.0): - model = CPL(backbone, prototype_count=prototype_count, lamb=lamb, temp=temp) - return model - - -__all__ = ["CPL", "create_model"] diff --git a/examples/healthcare/application/TED_CT_Detection/train.py b/examples/healthcare/application/TED_CT_Detection/train.py index 4994c2aaa..2b045fd93 100644 --- a/examples/healthcare/application/TED_CT_Detection/train.py +++ b/examples/healthcare/application/TED_CT_Detection/train.py @@ -26,13 +26,10 @@ from PIL import Image import sys +sys.path.append("../../..") -sys.path.append(".") -print(sys.path) - -import examples.cnn.model.cnn as cnn -from examples.cnn.data import cifar10 -import model as cpl +from healthcare.data import cifar10 +from healthcare.models import tedct_net def accuracy(pred, target): @@ -60,6 +57,7 @@ def resize_dataset(x, image_size): def run( local_rank, + dir_path, max_epoch, batch_size, sgd, @@ -68,18 +66,19 @@ def run( dist_option="plain", spars=None, ): - dev = device.create_cuda_gpu_on(local_rank) + # dev = device.create_cuda_gpu_on(local_rank) + dev = device.get_default_device() dev.SetRandSeed(0) np.random.seed(0) - train_x, train_y, val_x, val_y = cifar10.load() + train_x, train_y, val_x, val_y = cifar10.load(dir_path) num_channels = train_x.shape[1] data_size = np.prod(train_x.shape[1 : train_x.ndim]).item() num_classes = (np.max(train_y) + 1).item() - backbone = cnn.create_model(num_channels=num_channels, num_classes=num_classes) - model = cpl.create_model(backbone, prototype_count=10, lamb=0.5, temp=10) + backbone = tedct_net.create_cnn_model(num_channels=num_channels, num_classes=num_classes) + model = tedct_net.create_model(backbone, prototype_count=10, lamb=0.5, temp=10) if backbone.dimension == 4: tx = tensor.Tensor( @@ -139,6 +138,12 @@ def run( if __name__ == "__main__": parser = argparse.ArgumentParser(description="Train a CPL model") + parser.add_argument('-dir', + '--dir-path', + default="/tmp/cifar-10-batches-py", + type=str, + help='the directory to store the dataset', + dest='dir_path') parser.add_argument( "-m", "--max-epoch", @@ -187,5 +192,11 @@ def run( sgd = opt.SGD(lr=args.lr, momentum=0.9, weight_decay=1e-5) run( - args.device_id, args.max_epoch, args.batch_size, sgd, args.graph, args.verbosity - ) + args.device_id, + args.dir_path, + args.max_epoch, + args.batch_size, + sgd, + args.graph, + args.verbosity + ) \ No newline at end of file