77.安心技术梳理 - 开发中 - 异常- 类和方法的创建和使用,干货

开发中异常的输出是必要的,我们需要统一的数据标准,做个统一工具,仅供参考

1.实际应用的方式

(1)创建常量返回异常结果

import com.Result;
Result.failResult(CocoonErrorCode.PURCHASECARD_ACT_EXCEPTION, "付费卡活动异常")

(2)创建异常类返回

throw BusinessException.of(CocoonErrorCode.PAIDCOUPON_NOEXIT).tips("查询列表为空");

2.实现的工具类

(1)异常分类

public interface CocoonErrorCodePrefix {
    //通用
    String COMMON = "30";
}

(2)创建异常常量

import com.ErrorCode;
public class CocoonErrorCode extends ErrorCode {
    public CocoonErrorCode(String code, String message) {
        super(code, message);
    }
    @Override
    protected String getPrefix() {
        return CocoonErrorCodePrefix.COMMON;
    }
    public static final CocoonErrorCode NOEXIT = new CocoonErrorCode("50001", "没有活动");
    public static final CocoonErrorCode FRESHDATA_NOEXIT = new CocoonErrorCode("50002", "活动失效");

}

(3)创建异常类

import com.Result;
import java.io.Serializable;
import java.util.Objects;
public abstract class ErrorCode implements Serializable {
    //错误码
    private String code;
    //错误信息
    private String message;
    public ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }
    //错误码前缀
    protected abstract String getPrefix();
    public <T> Result<T> result() {
        return Result.failResult(this);
    }
    public <T> Result<T> result(String tips) {
        return Result.failResult(this, tips);
    }
    public String getCode() {
        return getPrefix() + code;
    }
    public String getMessage() {
        return message;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ErrorCode errorCode = (ErrorCode) o;
        return Objects.equals(code, errorCode.code);
    }
    @Override
    public int hashCode() {
        return Objects.hash(code);
    }
}

(4)返回异常方法,创建异常类

import com.ErrorCode;
import com.Result;
public class BusinessException extends RuntimeException {
    //异常码信息
    private ErrorCode errorCode;
    //提示信息
    private String tips;
    public BusinessException() {}
    public static BusinessException of(ErrorCode errorCode) {
        return new BusinessException(errorCode);
    }
    public static BusinessException of(ErrorCode errorCode, String tips) {
        return new BusinessException(errorCode, tips);
    }
    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }
    public BusinessException(ErrorCode errorCode, String tips) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
        this.tips = tips;
    }
    public BusinessException(ErrorCode errorCode, Throwable cause) {
        super(errorCode.getMessage(), cause);
        this.errorCode = errorCode;
    }
    public BusinessException tips(String tips) {
        this.tips = tips;
        return this;
    }
    public <T> Result<T> result() {
        return errorCode.result(tips);
    }
    public ErrorCode getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }
    public String getTips() {
        return tips;
    }
}

好的,很高兴为您解答这个问题。 在深度学习,数据增强是一种常用的技术,它可以通过对原始数据进行变换、裁剪、旋转、翻转等操作,来扩充训练集,从而提高模型的泛化能力和鲁棒性。在图像领域,常用的数据增强方法包括随机裁剪、随机旋转、随机翻转、色域变换等。下面,我将为您介绍如何使用 Python 对图像进行随机数据增强。 1. 随机裁剪 随机裁剪是一种简单而有效的数据增强方法,它可以通过对原始图像进行随机裁剪,来扩充训练集。在 Python ,我们可以使用 PIL 库的 Image 模块来实现随机裁剪。 ```python from PIL import Image import random def random_crop(img, crop_size): w, h = img.size left = random.randint(0, w - crop_size) top = random.randint(0, h - crop_size) right = left + crop_size bottom = top + crop_size return img.crop((left, top, right, bottom)) ``` 2. 随机旋转 随机旋转是一种常用的数据增强方法,它可以通过对原始图像进行随机旋转,来扩充训练集。在 Python ,我们可以使用 PIL 库的 Image 模块来实现随机旋转。 ```python from PIL import Image import random def random_rotate(img, angle_range): angle = random.uniform(-angle_range, angle_range) return img.rotate(angle) ``` 3. 随机翻转 随机翻转是一种简单而有效的数据增强方法,它可以通过对原始图像进行随机翻转,来扩充训练集。在 Python ,我们可以使用 PIL 库的 Image 模块来实现随机翻转。 ```python from PIL import Image import random def random_flip(img, flip_prob): if random.random() < flip_prob: return img.transpose(Image.FLIP_LEFT_RIGHT) return img ``` 4. 色域变换 色域变换是一种常用的数据增强方法,它可以通过对原始图像进行色彩变换,来扩充训练集。在 Python ,我们可以使用 PIL 库的 Image 模块来实现色域变换。 ```python from PIL import Image import random def random_color(img, color_range): r, g, b = img.split() r = r.point(lambda i: i + random.randint(-color_range, color_range)) g = g.point(lambda i: i + random.randint(-color_range, color_range)) b = b.point(lambda i: i + random.randint(-color_range, color_range)) return Image.merge('RGB', (r, g, b)) ``` 以上就是使用 Python 进行随机数据增强的一些常用方法,希望对您有所帮助。如果您有任何问题,请随时联系我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值