__va_rounded_size

本文介绍Linux内核中STDarg.h文件内的__va_rounded_size宏定义,该宏用于计算特定类型数据所占的字节数,并且是以int作为最小单位进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux内核STDarg.h 中 

 

#define __va_rounded_size(TYPE)  /

  (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))

  

求Type 类型所占有的字节数。以int为最小单元,

import os from datetime import datetime import tensorflow as tf import keras from tensorflow.keras.optimizers import Adam # 使用TensorFlow的Keras os.environ["SM_FRAMEWORK"] = "tf.keras" # 显式设置后端 os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' os.environ['TF_GPU_ALLOCATOR'] = 'cuda_malloc_async' os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices=false' # 导入 segmentation_models 并设置框架 import segmentation_models as sm sm.set_framework('tf.keras') # 确保使用 tf.keras 后端 import cv2 import numpy as np from matplotlib import pyplot as plt import matplotlib from keras.utils.np_utils import to_categorical from tensorflow.keras.utils import Sequence from sklearn.metrics import f1_score, precision_score, recall_score, roc_auc_score, matthews_corrcoef import pandas as pd from tqdm import tqdm import gc from keras.callbacks import EarlyStopping # 设置 matplotlib 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS'] plt.rcParams['axes.unicode_minus'] = False # 设置环境变量以优化 GPU 性能 os.environ["SM_FRAMEWORK"] = "tf.keras" os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' os.environ['TF_GPU_ALLOCATOR'] = 'cuda_malloc_async' os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices=false' # 设置全精度计算 tf.keras.mixed_precision.set_global_policy('float32') # 设置 GPU 内存增长 gpus = tf.config.list_physical_devices('GPU') if gpus: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) else: print("警告:未检测到 GPU,将使用 CPU 运行") # 打印环境信息 print("TensorFlow 版本:", tf.__version__) print("segmentation_models 版本:", sm.__version__) print("GPU 设备:", tf.config.list_physical_devices('GPU')) print("CUDA 可用:", tf.test.is_built_with_cuda()) print("cuDNN 可用:", tf.test.is_built_with_gpu_support()) # 设置随机种子 np.random.seed(0) tf.random.set_seed(0) # 配置参数 n_classes = 2 SIZE = 1024 # 使用第一份代码的尺寸 base_directory = 'D:/pycharm/spark/data/data/data_1024_1024/' # 划分后的数据集路径 train_directory = os.path.join(base_directory, 'train_data') val_directory = os.path.join(base_directory, 'val_data') test_directory = os.path.join(base_directory, 'test_data') result_directory = 'D:/pycharm/spark/data/data/result3/' checkpoint_directory = 'D:/pycharm/spark/data/data/duststorm/' os.makedirs(result_directory, exist_ok=True) os.makedirs(checkpoint_directory, exist_ok=True) # 检查目录 for directory in [train_directory, val_directory, test_directory]: img_dir = os.path.join(directory, 'mdgm1') mask_dir = os.path.join(directory, 'mask1') if not os.path.exists(img_dir) or not os.path.exists(mask_dir): print(f"错误:目录 {img_dir} 或 {mask_dir} 不存在") exit() # 数据生成器类 class DataGenerator(Sequence): def __init__(self, image_paths, mask_paths, preprocess_input, batch_size=4, size=SIZE, n_classes=2): self.image_paths = image_paths self.mask_paths = mask_paths self.preprocess_input = preprocess_input self.batch_size = batch_size self.size = size self.n_classes = n_classes def __len__(self): return max(1, int(np.ceil(len(self.image_paths) / self.batch_size))) def __getitem__(self, idx): batch_image_paths = self.image_paths[idx * self.batch_size:(idx + 1) * self.batch_size] batch_mask_paths = self.mask_paths[idx * self.batch_size:(idx + 1) * self.batch_size] images = np.empty((len(batch_image_paths), self.size, self.size, 3), dtype=np.float32) masks = np.empty((len(batch_image_paths), self.size, self.size), dtype=np.uint8) for i, (img_path, mask_path) in enumerate(zip(batch_image_paths, batch_mask_paths)): img = cv2.imread(img_path, cv2.IMREAD_COLOR) if img is None: print(f"警告:无法加载图像 {img_path}") continue img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (self.size, self.size), interpolation=cv2.INTER_NEAREST) images[i] = img mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE) if mask is None: print(f"警告:无法加载掩码 {mask_path}") continue mask = cv2.resize(mask, (self.size, self.size), interpolation=cv2.INTER_NEAREST) mask = np.where(mask > 0, 1, 0).astype(np.uint8) masks[i] = mask if not images.size or not masks.size: print(f"警告:批次 {idx} 为空,返回占位数据") return np.zeros((1, self.size, self.size, 3), dtype=np.float32), \ np.zeros((1, self.size, self.size, self.n_classes), dtype=np.float32) images = self.preprocess_input(images) masks = to_categorical(masks, num_classes=self.n_classes).astype(np.float32) return images, masks # 过滤图像文件 def is_image_file(filename): return filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')) # 加载文件路径 def load_paths(directory): img_dir = os.path.join(directory, 'mdgm1') mask_dir = os.path.join(directory, 'mask1') image_files = sorted([f for f in os.listdir(img_dir) if is_image_file(f)]) mask_files = sorted([f for f in os.listdir(mask_dir) if is_image_file(f)]) common_files = sorted(set(image_files) & set(mask_files)) image_paths = [os.path.join(img_dir, fname) for fname in common_files] mask_paths = [os.path.join(mask_dir, fname) for fname in common_files] print(f"{directory}:找到 {len(common_files)} 个匹配文件对") return image_paths, mask_paths, common_files # 加载数据集 train_image_paths, train_mask_paths, train_names = load_paths(train_directory) val_image_paths, val_mask_paths, val_names = load_paths(val_directory) test_image_paths, test_mask_paths, test_names = load_paths(test_directory) # 创建数据生成器 BACKBONE = 'inceptionv3' preprocess_input = sm.get_preprocessing(BACKBONE) train_generator = DataGenerator(train_image_paths, train_mask_paths, preprocess_input, batch_size=4) val_generator = DataGenerator(val_image_paths, val_mask_paths, preprocess_input, batch_size=4) test_generator = DataGenerator(test_image_paths, test_mask_paths, preprocess_input, batch_size=4) # 模型参数 activation = 'sigmoid' LR = 1e-5 # 使用第二份代码的学习率 optim = Adam(LR, clipnorm=1.0) metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)] loss = sm.losses.BinaryFocalLoss(alpha=0.25, gamma=2.0) + sm.losses.DiceLoss(class_weights=[0.2, 0.8]) # 加载权重 weights_path = 'D:/pycharm/spark/data/data/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5' try: if not os.path.exists(weights_path): print(f"错误:权重文件 {weights_path} 不存在,使用 ImageNet 权重") model = sm.Unet(BACKBONE, encoder_weights='imagenet', classes=n_classes, activation=activation) else: model = sm.Unet(BACKBONE, encoder_weights=weights_path, classes=n_classes, activation=activation) except Exception as e: print(f"加载权重失败: {e},使用随机初始化") model = sm.Unet(BACKBONE, encoder_weights=None, classes=n_classes, activation=activation) # 编译模型 with tf.device('/GPU:0'): model.compile(optim, loss=loss, metrics=metrics) print(model.summary()) # 设置回调 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") checkpoint_filepath = f'{checkpoint_directory}/train_MY30_31_inceptionv3_{timestamp}.keras' callbacks = [ tf.keras.callbacks.ModelCheckpoint( filepath=checkpoint_filepath, monitor='val_iou_score', mode='max', save_best_only=True, verbose=1 ), EarlyStopping( monitor='val_iou_score', mode='max', patience=5, restore_best_weights=True, verbose=1 ), tf.keras.callbacks.ReduceLROnPlateau( monitor='val_loss', factor=0.5, patience=3, min_lr=1e-6, verbose=1 ) ] # 训练模型 try: with tf.device('/GPU:0'): history = model.fit( train_generator, epochs=50, validation_data=val_generator, callbacks=callbacks, verbose=1 ) except Exception as e: print(f"训练失败: {e}") exit() # 绘制训练和验证曲线 def plot_metrics(history, metric_name, title, ylabel): metric = history.history[metric_name] val_metric = history.history[f'val_{metric_name}'] epochs = range(1, len(metric) + 1) plt.figure(figsize=(10, 5)) plt.plot(epochs, metric, 'y', label=f'训练 {ylabel}') plt.plot(epochs, val_metric, 'r', label=f'验证 {ylabel}') plt.title(title) plt.xlabel('轮次') plt.ylabel(ylabel) plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') plt.tight_layout() plt.show() plot_metrics(history, 'loss', '训练和验证损失 train_MY30_31_inceptionv3', '损失') plot_metrics(history, 'iou_score', '训练和验证 IoU train_MY30_31_inceptionv3', 'IoU') # 保存训练指标 with open(f'{checkpoint_directory}/train_MY30_31_inceptionv3_{timestamp}.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['训练损失', '验证损失', '训练 IoU', '验证 IoU']) for row in zip(history.history['loss'], history.history['val_loss'], history.history['iou_score'], history.history['val_iou_score']): writer.writerow(row) # 测试集评估 def calculate_iou(pred_mask, gt_mask): intersection = np.logical_and(pred_mask, gt_mask).sum() union = np.logical_or(pred_mask, gt_mask).sum() return intersection / union if union != 0 else 1.0 def pixel_accuracy(y_true, y_pred): y_pred = tf.round(y_pred) correct_pixels = tf.reduce_sum(tf.cast(tf.equal(y_true, y_pred), tf.float32)) total_pixels = tf.cast(tf.size(y_true), tf.float32) return (correct_pixels / total_pixels).numpy() def pixel_accuracy_for_class(y_true, y_pred, class_id): y_pred_rounded = tf.cast(tf.round(y_pred), tf.int32) true_mask = tf.equal(y_true, class_id) pred_mask = tf.equal(y_pred_rounded, class_id) correct_pixels_class = tf.reduce_sum(tf.cast(tf.logical_and(true_mask, pred_mask), tf.float32)) total_pixels_class = tf.reduce_sum(tf.cast(true_mask, tf.float32)) return (correct_pixels_class / total_pixels_class).numpy() if total_pixels_class != 0 else 0.0 # 测试集预测和评估 metrics_dict = { '文件名': [], 'IoU 分数': [], 'AUC 分数': [], 'F1 分数': [], 'F1 微平均': [], 'F1 宏平均': [], '精确率 (宏平均)': [], '召回率 (宏平均)': [], '像素精度 (整体)': [], '像素精度 (尘暴)': [], 'MCC': [] } for i in tqdm(range(len(test_image_paths)), desc="测试集预测"): test_img = cv2.imread(test_image_paths[i], cv2.IMREAD_COLOR) if test_img is None: print(f"警告:无法加载测试图像 {test_image_paths[i]}") continue test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2RGB) test_img = cv2.resize(test_img, (SIZE, SIZE), interpolation=cv2.INTER_NEAREST) test_img_input = preprocess_input(np.expand_dims(test_img, 0)) test_mask = cv2.imread(test_mask_paths[i], cv2.IMREAD_GRAYSCALE) if test_mask is None: print(f"警告:无法加载测试掩码 {test_mask_paths[i]}") continue test_mask = cv2.resize(test_mask, (SIZE, SIZE), interpolation=cv2.INTER_NEAREST) test_mask = np.where(test_mask > 0, 1, 0).astype(np.uint8) with tf.device('/GPU:0'): test_pred = model.predict(test_img_input, verbose=0) test_prediction = np.argmax(test_pred, axis=3)[0, :, :] iou = calculate_iou(test_prediction, test_mask) auc_score = roc_auc_score(test_mask.flatten(), test_pred[0, :, :, 1].flatten()) f1 = f1_score(test_mask.flatten(), test_prediction.flatten(), average='binary', zero_division=1) f1_micro = f1_score(test_mask.flatten(), test_prediction.flatten(), average='micro', zero_division=1) f1_macro = f1_score(test_mask.flatten(), test_prediction.flatten(), average='macro', zero_division=1) precision = precision_score(test_mask.flatten(), test_prediction.flatten(), average='macro', zero_division=0) recall = recall_score(test_mask.flatten(), test_prediction.flatten(), average='macro', zero_division=0) pa = pixel_accuracy(test_mask, test_prediction) pa_dust = pixel_accuracy_for_class(test_mask, test_prediction, 1) mcc = matthews_corrcoef(test_mask.flatten(), test_prediction.flatten()) metrics_dict['文件名'].append(test_names[i]) metrics_dict['IoU 分数'].append(iou) metrics_dict['AUC 分数'].append(auc_score) metrics_dict['F1 分数'].append(f1) metrics_dict['F1 微平均'].append(f1_micro) metrics_dict['F1 宏平均'].append(f1_macro) metrics_dict['精确率 (宏平均)'].append(precision) metrics_dict['召回率 (宏平均)'].append(recall) metrics_dict['像素精度 (整体)'].append(pa) metrics_dict['像素精度 (尘暴)'].append(pa_dust) metrics_dict['MCC'].append(mcc) # 可视化 plt.figure(figsize=(15, 5)) plt.subplot(1, 3, 1) plt.title('图像') plt.imshow(test_img) plt.axis('off') plt.subplot(1, 3, 2) plt.title('图像 + 掩码') plt.imshow(test_img) plt.imshow(test_mask, cmap='jet', alpha=0.5) plt.axis('off') plt.subplot(1, 3, 3) plt.title('预测 + 掩码 + IoU') plt.imshow(test_prediction, cmap='gray') plt.imshow(test_mask, cmap='jet', alpha=0.5) plt.text(0.5, -0.1, f'IoU: {iou:.3f}', ha='center', va='center', transform=plt.gca().transAxes, fontsize=10) plt.axis('off') plt.savefig(os.path.join(result_directory, f'test_{test_names[i]}'), bbox_inches='tight') plt.close() # 保存测试指标 pd.DataFrame(metrics_dict).to_csv(f'{checkpoint_directory}/test_MY30_31_inceptionv3_metrics_{timestamp}.csv', index=False) 现在是可以运行,但是运行不是用gpu运行,是使用cpu运行
最新发布
08-15
<think>我们正在讨论如何在Android中设置圆角半径。根据引用[1],每个角最初必须设置大于1的圆角半径,否则不会圆角。如果想特定角不圆角,可以设置一个默认大于1的半径,然后覆盖每个角为0(不圆角)或所需值。 引用[2]提到RoundedBitmapDrawable类,其中setCornerRadius方法可以设置圆角半径,setCircular方法可以设置为圆形。 引用[3]说明可以使用setCornerRadii方法分别设置每个角的半径,数组包含8个值(每个角两个值:X半径和Y半径),顺序为:左上、右上、右下、左下。 引用[4]提到一个开源项目RoundedImageView,但问题中未直接使用。 因此,设置圆角半径的方法有: 1. 在XML中使用shape drawable,通过corners元素设置。 2. 在代码中使用RoundedBitmapDrawable。 3. 在自定义View中通过setCornerRadii方法分别设置每个角。 现在,我将提供两种主要方法:XML方式和代码方式。 ### 方法1:XML方式(使用Drawable资源) 创建一个drawable资源文件(例如rounded_corner.xml): ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 设置圆角半径,可以分别设置四个角 --> <corners android:radius="8dp" <!-- 所有角相同的半径 --> android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/> <!-- 其他属性,如填充色等 --> <solid android:color="#FFFFFF" /> </shape> ``` 然后在View的背景中使用这个drawable: ```xml <View android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/rounded_corner"/> ``` 注意:如果只想设置一个角,其他角不圆角,需要设置一个默认半径(大于1)然后覆盖不需要的角为0dp,如引用[1]所述。 ### 方法2:代码方式(使用RoundedBitmapDrawable) 适用于将Bitmap转换为圆角图片。 ```java // 假设你有一个Bitmap对象 Bitmap bitmap = ...; // 创建RoundedBitmapDrawable对象 RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); // 设置圆角半径 roundedDrawable.setCornerRadius(20f); // 统一设置四个角 // 或者分别设置每个角(需要API 21+) // roundedDrawable.setCornerRadii(new float[] {20,20, 20,20, 0,0, 0,0}); // 左上、右上、右下、左下 // 设置为ImageView imageView.setImageDrawable(roundedDrawable); ``` ### 方法3:使用View的OutlineProvider(适用于API 21+) 对于View的背景,可以使用ViewOutlineProvider来设置圆角: ```java View view = findViewById(R.id.view); view.setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 20f); } }); view.setClipToOutline(true); ``` 注意:setClipToOutline(true)会裁剪视图内容为圆角。 ### 注意事项 - 如果使用setCornerRadii分别设置每个角,数组长度为8,每两个值表示一个角的X半径和Y半径(可以设置椭圆角)。 - 圆角半径单位在代码中为像素(px),在XML中为dp,注意转换。 ### 相关问题 1. 如何在Android中设置一个只有顶部圆角的布局? 2. 使用RoundedBitmapDrawable设置圆形图片和圆角图片有什么区别? 3. 在Android中,圆角图片的性能优化有哪些方法?</think>在 Android 中设置圆角半径(Rounded Corner Radius)主要有以下几种方法,根据您的需求选择合适的方式: ### 方法 1:XML 布局中设置(推荐) 在 `drawable` 目录创建 XML 文件(如 `rounded_corner_bg.xml`): ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 统一设置所有角半径 --> <corners android:radius="8dp" /> <!-- 或单独设置各角半径 --> <corners android:topLeftRadius="12dp" android:topRightRadius="12dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp" /> <solid android:color="#FF5722" /> <!-- 背景色 --> </shape> ``` 在布局文件中应用: ```xml <View android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/rounded_corner_bg" /> ``` > ⚠️ 注意:如果某角落不需要圆角,必须显式设置为 `0dp`,否则系统会忽略半径小于 `1dp` 的设置[^1]。 ### 方法 2:代码动态设置(使用 RoundedBitmapDrawable) ```java // 将 Bitmap 转换为圆角 Drawable Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image); RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); // 设置统一圆角半径 roundedDrawable.setCornerRadius(20f); // 或设置各角不同半径(API 21+) float[] radii = {20, 20, 15, 15, 0, 0, 5, 5}; // 顺序: 左上x,y 右上x,y 右下x,y 左下x,y roundedDrawable.setCornerRadii(radii); imageView.setImageDrawable(roundedDrawable); ``` > 说明:`setCornerRadii()` 参数是长度为 8 的数组,表示四个角的 X/Y 半径[^3]。 ### 方法 3:使用 MaterialCardView(Material 组件) ```xml <com.google.android.material.card.MaterialCardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="16dp" <!-- 统一圆角 --> app:shapeAppearanceOverlay="@style/CustomShapeAppearance"> <!-- 自定义各角 --> </com.google.android.material.card.MaterialCardView> ``` 在 `res/values/styles.xml` 中定义自定义样式: ```xml <style name="CustomShapeAppearance"> <item name="cornerFamily">rounded</item> <item name="cornerSizeTopLeft">8dp</item> <item name="cornerSizeBottomRight">20dp</item> </style> ``` ### 方法 4:使用开源库 RoundedImageView 1. 添加依赖: ```gradle implementation 'com.makeramen:roundedimageview:2.3.0' ``` 2. 在布局中使用: ```xml <com.makeramen.roundedimageview.RoundedImageView android:layout_width="100dp" android:layout_height="100dp" app:riv_corner_radius="10dp" app:riv_border_width="2dp" app:riv_border_color="#333" app:riv_oval="false" /> ``` 支持单独设置四个角半径:`riv_corner_radius_top_left` 等属性[^4]。 ### ⚠️ 重要注意事项 1. 最小半径值:系统默认忽略小于 `1dp` 的圆角设置,如需锐角需显式设置 `0dp`[^1] 2. 性能优化:对大量圆角视图建议使用 `View.setClipToOutline(true)` 开启硬件加速 3. 图片变形:圆角图片可能被拉伸,建议使用 `android:scaleType="centerCrop"` > 引用说明:上述方法综合了 Android 原生 API 和常见第三方解决方案,其中圆角半径的边界处理参考了 Android 官方文档的特别说明[^1],`RoundedBitmapDrawable` 的使用参考了其 API 定义[^2][^3],开源方案参考了社区实践[^4]。 ### 相关问题 1. 如何在 Android 中实现只有顶部圆角的布局? 2. 设置圆角后图片边缘出现锯齿如何解决? 3. MaterialCardView 和普通 View 的圆角性能有何差异? 4. 如何为 RecyclerView 的列表项动态设置不同圆角效果?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值